小编MDA*_*Dev的帖子

EF 4.3 Code First:具有复合主键和外键的每类型表(TPT)

所以我正在尝试使用Code First和Fluent来映射具有一种派生类型的基类,其中表模式是Table-per-Type排列.此外,派生类型与具有复合外键的另一种类型具有多对一关系.(这些表上的键是不可更改的,名称完全匹配.)

以下是我在CSharp中尝试实现的示例:

public class BaseType
{
    public int Id;
    public int TenantId;
    public int Name;
}

public class DerivedType : BaseType
{
    public int Active;
    public int OtherTypeId;
    public OtherType NavigationProperty; 
}
Run Code Online (Sandbox Code Playgroud)

以下是配置类中的配置:

public BaseTypeConfiguration()
{
     ToTable("BaseTypes", "dbo");

     HasKey(f => new { f.Id, f.TenantId});

     Property(f => f.Id)
         .HasColumnName("BaseTypeId")
         .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}

public DerivedTypeConfiguration()
{
    ToTable("DerivedTypes", "dbo");

    //OtherType has many DerivedTypes
    HasRequired(dt=> dt.OtherTypeNavigation)
        .WithMany(ot=> ot.DerivedTypes)
        .HasForeignKey(dt=> new { dt.OtherTypeId, dt.TenantId});
}
Run Code Online (Sandbox Code Playgroud)

从我可以告诉我的映射是正确设置的(例如,我遵循了许多具有这种确切情况但具有单个列标识符的教程和示例)

当我尝试查询这些实体时,我得到的异常是: The foreign key component 'TenantId' is not a …

c# table-per-type composite-primary-key ef-code-first entity-framework-4.3

6
推荐指数
1
解决办法
1611
查看次数