我正在使用 Entity Framework 4.3.1 Code-First,我需要在两个表之间拆分一个实体。这些表有一个共享的主键,并且是一对一的,但每个表上的列名称不同。
我不控制数据布局,也不能请求任何更改。
例如,SQL 表可以是
这将是我的实体......
public class MyEntity
{
public int Id {get; set;}
public string Name {get;set}
public string FromAnotherTable {get;set;}
}
这是我的映射。
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");
});
}
由于它们之间共享的键具有不同的列名,我不确定如何映射它。此映射将编译,但在运行时失败,因为 EF 发出 SQL 来查找“ExtendedTable”表上的“ThePrimaryKeyId”列,该表不存在。
编辑 为了澄清,如果“扩展表”上的 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]
但它似乎唯一想要发出的东西是
SELECT
[e1].*,
[e2].*
FROM [MainTable] AS [e1]
INNER JOIN [ExtendedTable] AS [e2]
ON [e1].[ThePrimaryKeyId] = [e2].[ThePrimaryKeyId] /* this column doesn't exist */
编辑 我在 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; }
}
这是映射类
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);
}
}
此设置收到消息
"Column or attribute 'MyEntity_ThePrimaryKeyId' is not defined in 'ExtendedTable'"
将最终地图线更改为
this.HasRequired(e => e.MainEntry).WithRequiredDependent(f => f.ExtendedProperties).Map(m => M.MapKey("NotTheSameName"));
返回此消息
"Each property name in a type must be unique. property name 'NotTheSameName' was already defined."
更改映射键以使用父表中的列,MapKey("ThePrimaryKeyId")
. 返回此消息
"Column or attribute 'ThePrimaryKeyId' is not defined in 'ExtendedTable'"
从类中删除Id
属性ExtEntity
会引发错误,因为实体没有定义的键。