【问题标题】:Entity Framework - TPH Inheritance with Null Database Fields实体框架 - 具有空数据库字段的 TPH 继承
【发布时间】:2014-11-22 06:00:34
【问题描述】:

我有 2 个继承自同一个基类的子类。它们都映射回同一张表。如果 db 表中的 field1 为 null 而 field2 不为 null,则它是一个子类。如果 field1 不为 null,而 field 2 为 null,则为另一个子类。

我不断收到错误消息。实际消息是:“列名‘鉴别器’无效。”它实际上说的是鉴别器......我没有把它作为一个通用术语。

这是我的代码示例:

DatabaseTableA
TableAId ( PK , int )
FooId ( FK , int , null )
BarId ( FK , int , null )
Prop1 ( int )
Prop2 ( int )
Prop3 ( int )


public abstract class BaseClass
{
    public int Prop1{ get; set; }
    public int Prop2{ get; set; }
    public int Prop3{ get; set; }
}

public class Foo : BaseClass
{
    public int FooId{get;set;}
}

public class Bar : BaseClass
{
    public int BarId{get;set;}
}

internal class BaseClassMap : EntityTypeConfiguration<BaseClass>
{
    public BaseClassMap()
    {
        ToTable("DatabaseTableA");
        HasKey( e => e.TableAId);

        Map<Foo>( m => m.Requires("BarId").IsDBNull());
        Map<Bar>( m => m.Requires("FooId").IsDBNull());
    }
}

如何正确映射?

【问题讨论】:

    标签: entity-framework inheritance tph


    【解决方案1】:

    实体框架将假定任何继承自映射到数据库表的 POCO 类的类都需要一个鉴别器列,即使派生类不会保存到数据库。

    在此处查看解决方案EF Code First "Invalid column name 'Discriminator'" but no inheritance

    【讨论】:

    • 我之前确实阅读过该解决方案。我看不出这如何解决我的问题,所有的列都在子类中。
    猜你喜欢
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多