@slauma在评论中给出了一个重要的事实。所以看看答案和评论!
我正在尝试使用实际与NHibernate一起使用的组件和EF6。问题是,我有一些TPT继承与不同名称的主键。给出了数据库和POCO类,我无法更改它们,因此CodeFirst和EF设计器都是不可能的。
有没有办法将现有的Db映射到现有的POCO类,就像在NHibernate中使用这些.hbm.xml映射文件一样?
更新
我遇到的实际问题首先是几个类的TPT映射,其中这些类具有不同名称的主键,代码似乎不首先支持。
所以喜欢:
public class Record
{
public virtual int Ndx { get; set; } // table column 'ndx'
public virtual DateTime CreatedAt { get; set; } // table column 'created'
// ... further properties
}
public class Patient : Record
{
public virtual int RecordNdx {get; set;} // table column 'record_ndx) with FK => records.ndx
// ... further properties
}
如前所述,更改属性或列名称不是一种选择。
更新II:
这是我的注册码:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Record>()
.ToTable("record_descriptors", "schema");
modelBuilder.Entity<Record>()
.HasKey<int>(e => e.ndx);
modelBuilder.Entity<Record>()
.Property(e => e.read_flag)
.IsFixedLength()
.IsUnicode(false);
modelBuilder.Entity<Record>()
.Property(e => e.row_version)
.IsFixedLength();
modelBuilder.Entity<Record>()
.Property(e => e.update_info)
.IsUnicode(false);
modelBuilder.Entity<Patient>()
.ToTable("patienten", "schema");
modelBuilder.Entity<Patient>()
.Property(e => e.mpi)
.IsUnicode(false);
modelBuilder.Entity<Patient>()
.Property(e => e.ndx)
.HasColumnName("record_ndx");
modelBuilder.Entity<Patient>()
.Ignore(r => r.RecordNdx);
}
更新III
对于测试,我使用:
db.patients.First(p => p.Ndx == 6040);
这会产生以下SQL(由于真实记录和患者类而更广泛):
SELECT
[Limit1].[C1] AS [C1],
[Limit1].[ndx] AS [ndx],
[Limit1].[owner_user_object_ndx] AS [owner_user_object_ndx],
[Limit1].[creator_department_user_object_ndx] AS [creator_department_user_object_ndx],
[Limit1].[creator_user_user_object_ndx] AS [creator_user_user_object_ndx],
[Limit1].[created] AS [created],
[Limit1].[read_flag] AS [read_flag],
[Limit1].[last_update] AS [last_update],
[Limit1].[last_update_user] AS [last_update_user],
[Limit1].[last_update_department] AS [last_update_department],
[Limit1].[freitext] AS [freitext],
[Limit1].[row_version] AS [row_version],
[Limit1].[update_info] AS [update_info],
[Limit1].[mpi] AS [mpi]
FROM ( SELECT TOP (1)
[Extent1].[ndx] AS [ndx],
[Extent1].[mpi] AS [mpi],
[Extent2].[owner_user_object_ndx] AS [owner_user_object_ndx],
[Extent2].[creator_department_user_object_ndx] AS [creator_department_user_object_ndx],
[Extent2].[creator_user_user_object_ndx] AS [creator_user_user_object_ndx],
[Extent2].[created] AS [created],
[Extent2].[read_flag] AS [read_flag],
[Extent2].[last_update] AS [last_update],
[Extent2].[last_update_user] AS [last_update_user],
[Extent2].[last_update_department] AS [last_update_department],
[Extent2].[freitext] AS [freitext],
[Extent2].[row_version] AS [row_version],
[Extent2].[update_info] AS [update_info],
'0X0X' AS [C1]
FROM [schema].[patienten] AS [Extent1]
INNER JOIN [schema].[record_descriptors] AS [Extent2] ON [Extent1].[ndx] = [Extent2].[ndx]
WHERE 6040 = [Extent1].[ndx]
) AS [Limit1]
这是错误的,因为它从[ndx]
中选择[patienten]
(必须是record_ndx
)并尝试加入[ndx]
答案 0 :(得分:1)
关于关闭this work item at CodePlex的说法声称,自从EF 6 定义TPT映射中父和子实体的不同键列名称后,与Code-First一起使用。如果确实如此,则以下Code-First映射应允许映射模型和数据库:
modelBuilder.Entity<Record>()
.ToTable("YourRecordTableName");
modelBuilder.Entity<Record>()
.HasKey(r => r.Ndx);
modelBuilder.Entity<Record>()
.Property(r => r.Ndx)
.HasColumnName("ndx"); // probably redundant because case doesn't matter
modelBuilder.Entity<Record>()
.Property(r => r.CreatedAt)
.HasColumnName("created");
modelBuilder.Entity<Patient>()
.ToTable("YourPatientTableName");
modelBuilder.Entity<Patient>()
.Property(r => r.Ndx) // Yes, no typo: It must be Ndx, NOT RecordNdx !
.HasColumnName("record_ndx");
modelBuilder.Entity<Patient>()
.Ignore(r => r.RecordNdx);
最后一个映射(忽略RecordNdx
属性)很重要。这意味着您的关键属性将为Patient.Ndx
。我不认为您可以将派生类中的任何属性作为关键属性。 key属性必须始终位于继承层次结构的基类中。但是,此属性可以映射两次(或者通常在TPT继承链中每个实体一次)到每个表的不同列名 - 从EF 6开始。
完全摆脱RecordNdx
属性将是最干净的解决方案。但既然你说你无法触及你的属性,那么将RecordNdx
的值与Ndx
属性相结合至少是有意义的(如果你可以改变属性getter和setter):
public virtual int RecordNdx
{
get { return Ndx; }
set { Ndx = value; }
}
修改强>
我刚用EF 6.1测试了上面的Code-First映射,确实有效! Record
表中的主键列为ndx
,Patient
表中的主键列为record_ndx
。在这些之间创建了TPT映射所需的一对一关系。
修改2
这是我使用的完整测试程序(当前的EF 6.1 Nuget包,.NET 4.5,VS 2012,SQL Server 2012 Express):
using System;
using System.Data.Entity;
namespace EFTPT6
{
public class Record
{
public virtual int Ndx { get; set; }
public virtual DateTime CreatedAt { get; set; }
}
public class Patient : Record
{
public virtual int RecordNdx { get; set; }
public virtual string Name { get; set; }
}
public class MyContext : DbContext
{
public DbSet<Record> Records { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Record>()
.ToTable("Records");
modelBuilder.Entity<Record>()
.HasKey(r => r.Ndx);
modelBuilder.Entity<Record>()
.Property(r => r.Ndx)
.HasColumnName("ndx");
modelBuilder.Entity<Record>()
.Property(r => r.CreatedAt)
.HasColumnName("created");
modelBuilder.Entity<Patient>()
.ToTable("Patients");
modelBuilder.Entity<Patient>()
.Property(r => r.Ndx)
.HasColumnName("record_ndx");
modelBuilder.Entity<Patient>()
.Ignore(p => p.RecordNdx);
}
}
class Program
{
static void Main(string[] args)
{
Database.SetInitializer(new DropCreateDatabaseAlways<MyContext>());
using (var ctx = new MyContext())
{
ctx.Database.Initialize(true);
string sql = ctx.Records.ToString();
}
}
}
}
程序末尾的字符串sql
是:
SELECT
CASE WHEN ( NOT (([Project1].[C1] = 1) AND ([Project1].[C1] IS NOT NULL)))
THEN '0X'
ELSE '0X0X'
END AS [C1],
[Extent1].[ndx] AS [ndx],
[Extent1].[created] AS [created],
CASE WHEN ( NOT (([Project1].[C1] = 1) AND ([Project1].[C1] IS NOT NULL)))
THEN CAST(NULL AS varchar(1))
ELSE [Project1].[Name]
END AS [C2]
FROM [dbo].[Records] AS [Extent1]
LEFT OUTER JOIN (SELECT
[Extent2].[record_ndx] AS [record_ndx],
[Extent2].[Name] AS [Name],
cast(1 as bit) AS [C1]
FROM [dbo].[Patients] AS [Extent2] ) AS [Project1]
ON [Extent1].[ndx] = [Project1].[record_ndx]
看起来映射是受到尊重的,即Records
和Patients
列由ndx
和record_ndx
列连接。
编辑3
上下文类不包含派生实体的集合,即无public DbSet<Patient> Patients { get; set; }
,这一点非常重要。如果是,则忽略映射modelBuilder.Entity<Patient>().Property(r => r.Ndx).HasColumnName("record_ndx");
,并且EF期望Patient
中的主键名称为ndx
而不是record_ndx
。例如,上面SQL中的最后一行变为ON [Extent1].[ndx] = [Project1].[ndx]
。