Entity framework 使用EF中的具体基和派生的具体类型编写第一个TPC继承

Entity framework 使用EF中的具体基和派生的具体类型编写第一个TPC继承,entity-framework,entity-framework-4.1,ef-code-first,Entity Framework,Entity Framework 4.1,Ef Code First,我正在尝试使用Code First建立TPC继承,以对传入和传出消息以及其中的记录进行建模。 基类型SentRecord是具体的,其派生类型ReceivedRecord也是具体的,继承自SentRecord,并添加了一些额外的字段以记录返回代码。类似于此,但具有更多属性: public class SentRecord : RecordBase { public int Id { get; set; } public string FooField { get; set; } }

我正在尝试使用Code First建立TPC继承,以对传入和传出消息以及其中的记录进行建模。 基类型SentRecord是具体的,其派生类型ReceivedRecord也是具体的,继承自SentRecord,并添加了一些额外的字段以记录返回代码。类似于此,但具有更多属性:

public class SentRecord : RecordBase {
    public int Id { get; set; }
    public string FooField { get; set; }
}

public class ReceivedRecord : SentRecord {
    public int ReturnCode { get; set; }
    public SentRecord SentRecord { get; set; }
}
当前模型是TPH,因此表会获得一个描述符列来标识持久化的对象类型。它可以工作,但我更希望两个对象都存储在单独的表中,而不需要鉴别器列。表SentRecord将只有列Id和FooField,表ReceivedRecord将有Id、FooField、ReturnCode和FK to SentRecord

我的DataContext类中当前有以下内容:

public class Context : DContext {
    public DbSet<SentRecord> SentRecords { get; set; }
    public DbSet<ReceivedRecord> ReceivedRecords { get; set; }
}
我不知道该怎么做才能以我上面描述的TPC方式设置它?或者我应该坚持使用TPH哪一种有效


提前谢谢

好的,我把它安装好并运行起来了。说实话,我给出的示例比我正在使用的实际类和继承层次结构要简单一点。该层次结构包含许多抽象类和具体类,其他类从中继承

通过减少继承来“扁平化”层次结构,使其工作平稳,没有任何错误。响应消息不再从发送的消息继承

简短版本:在尝试使用代码优先数据库模型时,不要使用混合了具体和抽象基类型的复杂继承树。坚持下去只会让事情变得更复杂

public class ReceivedRecord_Configuration : EntityTypeConfiguration<ReceivedRecord>{
    public ReceivedRecord_Configuration() {
        this.Map(m => {
            m.MapInheritedProperties();
            m.ToTable("ReceivedRecords");
        });
    }
}
public class SentRecord_Configuration : EntityTypeConfiguration<SentRecord>{
    public SentRecord_Configuration() {
        this.Map(m => {
            m.MapInheritedProperties(); //In order to map the properties declared in RecordBase
            m.ToTable("SentRecords");
        });
    }
}
Problem in mapping fragments starting at lines 455, 1284:
An entity from one EntitySet is mapped to a row that is also mapped to an entity from another EntitySet with possibly different key.
Ensure these two mapping fragments do not map two unrelated EntitySets to two overlapping groups of rows.