Ste*_*uer 6 c# inheritance entity-framework ef-code-first entity-framework-6
我有一个使用现有数据库的应用程序,目前使用NHibernate作为O/R-Mapper.
现在,我需要使用Code First和Fluent API Configuration迁移到Entity Framework 6.1.1.
但是现在我对部分数据模型有问题,因为它使用不同类型的继承策略(TPT和TPH)
注意:在这里发布完整的数据模型对我来说似乎有点太大了,所以我在一个小的POC程序中重现了我面临的问题.
CLASS | TABLE | TYPE
-----------------------+--------------------+------
BaseEntity (abstract) | BaseTable |
Inherited_TPH | BaseTable | 1
Inherited_TPT | Inherited_TPT | 2
Run Code Online (Sandbox Code Playgroud)
调用表中用作描述符的列 Type
根据这个答案,我添加了一个抽象类Intermediate_TPH作为中间层:

一些示例数据:带有ID=3类型的条目Inherited_TPT

这些是我的实体类和我的上下文类:
class MyContext : DbContext
{
public MyContext ( string connectionString )
: base ( connectionString )
{
}
public DbSet<Inherited_TPH> TPH_Set { get; set; }
public DbSet<Inherited_TPT> TPT_Set { get; set; }
public DbSet<SomethingElse> Another_Set { get; set; }
protected override void OnModelCreating ( DbModelBuilder modelBuilder )
{
modelBuilder
.Entity<BaseEntity> ()
.ToTable ( "BaseTable" );
modelBuilder
.Entity<Inherited_TPH> ()
.Map ( t => t.Requires ( "Type" ).HasValue ( 1 ) );
modelBuilder
.Entity<Intermediate_TPT> ()
.Map ( t => t.Requires ( "Type" ).HasValue ( 2 ) );
modelBuilder
.Entity<Intermediate_TPT> ()
.Map<Inherited_TPT> ( t => t.ToTable ( "Inherited_TPT" ) );
modelBuilder
.Entity<SomethingElse> ()
.ToTable ( "SomethingElse" )
.HasKey ( t => t.Id );
}
}
public abstract class BaseEntity
{
public virtual int Id { get; set; }
public virtual string Title { get; set; }
}
public class Inherited_TPH : BaseEntity
{
}
public abstract class Intermediate_TPT : BaseEntity
{
}
public class Inherited_TPT : Intermediate_TPT
{
public virtual string Comment { get; set; }
}
public class SomethingElse
{
public virtual string Description { get; set; }
public virtual int Id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
运行以下代码将给我一个错误.
static void Main ( string[] args )
{
Database.SetInitializer<MyContext> ( null );
var ctx = new MyContext ( @"Data Source=(local);Initial Catalog=nh_ef;Integrated Security=true" );
try
{
// Accessing Inherited_TPH works just fine
foreach ( var item in ctx.TPH_Set ) Console.WriteLine ( "{0}: {1}", item.Id, item.Title );
// Accessing Inherited_TPT works just fine
foreach ( var item in ctx.TPT_Set ) Console.WriteLine ( "{0}: {1} ({2})", item.Id, item.Title, item.Comment );
// The rror occurs when accessing ANOTHER entity:
foreach ( var item in ctx.Another_Set ) Console.WriteLine ( "{0}: {1}", item.Id, item.Description );
}
catch ( Exception ex )
{
Console.WriteLine ( ex.Message );
if( ex.InnerException != null ) { Console.WriteLine ( ex.InnerException.Message ); }
}
}
Run Code Online (Sandbox Code Playgroud)
该程序产生以下输出:
1:Simpson
2:Johnson
3:Smith(关于SMITH的更多细节)
4:Miller(关于MILLER的更多细节)
准备命令定义时发生错误.有关详细信息,请参阅内部异常(26,10):错误3032:从第14,26行开始映射片段的问题:EntityTypes PoC.Inherited_TPH,PoC.Inherited_TPT被映射到表BaseEntity中的相同行.映射条件可用于区分这些类型映射到的行.
如您所见,映射似乎有效,因为我可以从Inherited_TPT和加载所有数据Inherited_TPH.但是当访问另一个实体时,我得到一个例外.
我如何配置映射以摆脱此错误并能够访问现有的数据库结构?
我终于自己找到了解决方案。我会将其发布在这里,以防其他人需要相同的行为......
所以我们需要做的就是改变映射的定义:
protected override void OnModelCreating ( DbModelBuilder modelBuilder )
{
// Not changed
modelBuilder
.Entity<BaseEntity> ()
.ToTable ( "BaseTable" );
// --- CHANGED ---
modelBuilder.Entity<BaseEntity> ()
// TPH => Discriminator
.Map<Inherited_TPH> ( m => m.Requires ( "Type" ).HasValue ( 1 ).IsOptional () )
// TPT => Mapping to table
.Map<Inherited_TPT> ( m => m.ToTable ( "Inherited_TPT" ) );
// Not changed
modelBuilder
.Entity<SomethingElse> ()
.ToTable ( "SomethingElse" )
.HasKey ( t => t.Id );
}
Run Code Online (Sandbox Code Playgroud)