Jos*_*osh 12 c# entity-framework ef-code-first entity-framework-4.3
我正在使用Entity Framework 4.3.1 Code-First,我需要在两个表之间拆分实体.这些表共享一个主键,它是1对1,但每个表上的列名称不相同.
我不控制数据布局,也不能请求任何更改.
例如,SQL表可以是

这将是我的实体......
public class MyEntity
{
public int Id {get; set;}
public string Name {get;set}
public string FromAnotherTable {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
这是我的映射.
public class MyEntityMapping : EntityTypeConfiguration<MyEntity>
{
public MyEntityMapping()
{
this.Property(e => e.Id).HasColumnName("ThePrimaryKeyId");
this.Property(e => e.Name).HasColumnName("MyDatabaseName");
this.Property(e => e.FromAnothertable).HasColumnName("AnotherTableColumn");
this.Map(m =>
{
m.Properties(e =>
{
e.Id,
e.Name
});
m.ToTable("MainTable");
});
this.Map(m =>
{
m.Properties(e =>
{
e.Id,
e.FromAnotherTable
});
m.ToTable("ExtendedTable");
});
}
Run Code Online (Sandbox Code Playgroud)
由于它们之间共享的密钥具有不同的列名,因此我不确定如何映射它.此映射将编译,但在运行时失败,因为EF发出SQL查找"ExtendedTable"表上的"ThePrimaryKeyId"列,该列不存在.
编辑 为了澄清,如果"ExtendedTable"上的PK遵循命名约定,我上面定义的内容可以(并且确实)起作用.但它没有,我无法改变架构.
基本上,我需要EF发出的是一个类似的SQL语句
SELECT
[e1].*, /*yes, wildcards are bad. doing it here for brevity*/
[e2].*
FROM [MainTable] AS [e1]
INNER JOIN [ExtendedTable] AS [e2] /*Could be left join, don't care. */
ON [e1].[ThePrimaryKeyId] = [e2].[NotTheSameName]
Run Code Online (Sandbox Code Playgroud)
但它似乎唯一想要发出的是
SELECT
[e1].*,
[e2].*
FROM [MainTable] AS [e1]
INNER JOIN [ExtendedTable] AS [e2]
ON [e1].[ThePrimaryKeyId] = [e2].[ThePrimaryKeyId] /* this column doesn't exist */
Run Code Online (Sandbox Code Playgroud)
编辑 我在NSGaga的建议中再次尝试了一对一的方法.它不起作用,但结果如下.实体
public class MyEntity
{
public int Id { get; set; }
public int Name { get; set; }
public virtual ExtEntity ExtendedProperties { get; set; }
}
public class ExtEntity
{
public int Id { get; set; }
public string AnotherTableColumn { get; set; }
public virtual MyEntity MainEntry { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是映射类
public class MyEntityMapping : EntityTypeConfiguration<MyEntity>
{
public MyEntityMapping()
{
this.Property(e => e.Id).HasColumnName("ThePrimaryKeyId");
this.Property(e => e.Name).HasColumnName("MyDatabaseName");
this.ToTable("MainTable");
this.HasKey(e => e.Id);
this.HasRequired(e => e.ExtendedProperties).WithRequiredPrincipal(f => f.MainEntry);
}
}
public class ExtEntityMapping : EntityTypeConfiguration<ExtEntity>
{
public ExtEntityMapping()
{
this.Property(e => e.Id).HasColumnName("NotTheSameName");
this.Property(e => e.AnotherTableColumn).HasColumnName("AnotherTableColumn");
this.ToTable("ExtendedTable");
this.HasKey(e => e.Id);
this.HasRequired(e => e.MainEntry).WithRequiredDependent(f => f.ExtendedProperties);
}
}
Run Code Online (Sandbox Code Playgroud)
此设置获取消息
"Column or attribute 'MyEntity_ThePrimaryKeyId' is not defined in 'ExtendedTable'"
Run Code Online (Sandbox Code Playgroud)
将最终地图线更改为
this.HasRequired(e => e.MainEntry).WithRequiredDependent(f => f.ExtendedProperties).Map(m => M.MapKey("NotTheSameName"));
Run Code Online (Sandbox Code Playgroud)
返回此消息
"Each property name in a type must be unique. property name 'NotTheSameName' was already defined."
Run Code Online (Sandbox Code Playgroud)
更改映射键以使用父表中的列,MapKey("ThePrimaryKeyId").返回此消息
"Column or attribute 'ThePrimaryKeyId' is not defined in 'ExtendedTable'"
Run Code Online (Sandbox Code Playgroud)
Id从ExtEntity类中删除属性会引发错误,因为实体没有定义的键.
我找不到任何明确说明两个表中的列名称必须相同的内容;但我也找不到任何说明它不存在的内容,或者解释如何映射该场景。我能找到的每个示例在两个表中都具有相同名称的键。在我看来,这是 DbContext 设计中的一个漏洞。
| 归档时间: |
|
| 查看次数: |
6367 次 |
| 最近记录: |