C# 使用Entity Framework 6在两个不同的鉴别器字段上实现嵌套TPH

C# 使用Entity Framework 6在两个不同的鉴别器字段上实现嵌套TPH,c#,entity-framework,entity-framework-6,code-first,tph,C#,Entity Framework,Entity Framework 6,Code First,Tph,我正在使用EF6,首先从edmx迁移到代码。我从一个现有的数据库中做了一个反向工程,我试图复制我以前的模型,在一个名为“Contacts”的表上做一个嵌套的TPH,它有两个不同的字段“Type”和“SubType”,分别作为一级和二级鉴别器 我想要这样的东西: Contact Model (Type = 1) BookingModel (SubType = 1) ScoutingModel (Subtype = 2) Customer (Type

我正在使用EF6,首先从edmx迁移到代码。我从一个现有的数据库中做了一个反向工程,我试图复制我以前的模型,在一个名为“Contacts”的表上做一个嵌套的TPH,它有两个不同的字段“Type”和“SubType”,分别作为一级和二级鉴别器

我想要这样的东西:

Contact
    Model (Type = 1)
        BookingModel (SubType = 1)
        ScoutingModel (Subtype = 2)
    Customer (Type = 2)
        Agency (SubType = 3)
        Client (Subtype = 4)
        Service (Subtype = 5)
我将联系人、模型和客户类编码为抽象,将BookingModel、ScoutingModel、Agency、Client、Service编码为具体,但不包括“Type”和“Subtype”字段,因为它们是鉴别器

以下是映射代码:

public virtual DbSet<Contact> Contacts { get; set; }


    modelBuilder.Entity<Contact>()
            .Map<Model>(e => e.Requires("Type").HasValue((byte)ContactTypeEnum.Model))
            .Map<Customer>(e => e.Requires("Type").HasValue((byte)ContactTypeEnum.Customer));

            modelBuilder.Entity<Customer>()
                .Map<Agency>(e => {
                    e.Requires("Type").HasValue((byte)ContactTypeEnum.Customer);
                    e.Requires("SubType").HasValue((byte)CustomerTypeEnum.Agency);
                })
                .Map<Client>(e => {
                    e.Requires("Type").HasValue((byte)ContactTypeEnum.Customer);
                    e.Requires("SubType").HasValue((byte)CustomerTypeEnum.Client);
                })
                .Map<Service>(e => {
                    e.Requires("Type").HasValue((byte)ContactTypeEnum.Customer);
                    e.Requires("SubType").HasValue((byte)CustomerTypeEnum.Service);
                });

            modelBuilder.Entity<Model>()
                .Map<BookingModel>(e => {
                    e.Requires("Type").HasValue((byte)ContactTypeEnum.Model);
                    e.Requires("SubType").HasValue((byte)ModelTypeEnum.Booking);
                })
                .Map<ScoutingModel>(e => {
                    e.Requires("Type").HasValue((byte)ContactTypeEnum.Model);
                    e.Requires("SubType").HasValue((byte)ModelTypeEnum.Scouting);
                });
公共虚拟数据库集联系人{get;set;}
modelBuilder.Entity()
.Map(e=>e.Requires(“Type”).HasValue((字节)ContactTypeEnum.Model))
.Map(e=>e.Requires(“Type”).HasValue((字节)ContactTypeEnum.Customer));
modelBuilder.Entity()
.Map(e=>{
e、 需要(“类型”).HasValue((字节)ContactTypeEnum.Customer);
e、 需要(“子类型”).HasValue((字节)CustomerTypeEnum.Agency);
})
.Map(e=>{
e、 需要(“类型”).HasValue((字节)ContactTypeEnum.Customer);
e、 需要(“子类型”).HasValue((字节)CustomerTypeEnum.Client);
})
.Map(e=>{
e、 需要(“类型”).HasValue((字节)ContactTypeEnum.Customer);
e、 需要(“子类型”).HasValue((字节)CustomerTypeEnum.Service);
});
modelBuilder.Entity()
.Map(e=>{
e、 需要(“类型”).HasValue((字节)ContactTypeEnum.Model);
e、 需要(“子类型”).HasValue((字节)ModelTypeEnum.Booking);
})
.Map(e=>{
e、 需要(“类型”).HasValue((字节)ContactTypeEnum.Model);
e、 需要(“子类型”).HasValue((字节)ModelTypeEnum.Scouting);
});
但这不起作用,我犯了一个错误

(52,10):错误3023:映射从第52、68、75、82、89、98、106、729、747行开始的片段时出现问题:列接触。类型没有默认值,不可为null。存储实体数据需要列值

数据库中的“Type”和“SubType”字段不可为空,没有默认值。我还尝试将它们更改为可为null或提供默认值,但这不会更改结果错误


我在设置映射时是做错了什么,还是选择了一种错误的方式来面对这种情况?

您好,您是否设法解决了这个问题?我也面临同样的问题。我尝试了几种解决方案,但都没有成功。在我看来,代码优先的方法缺少在模型级别设置默认值的机会。