实体框架:TPC层次结构中的一对一问题

时间:2013-06-10 08:11:50

标签: entity-framework one-to-one table-per-hierarchy

我有一个像这样的TPC层次结构:

    public abstract class Base
{
    public Guid BaseId { get; set; }
    public string BaseName { get; set; }
}
public class Sub1 : Base
{
    public Sub2 Vender { get; set; }
}
public class Sub2 : Base
{
    public Sub1 Customer { get; set; }
}
public class MyDbContext : DbContext
{
    public MyDbContext()
        : base("name=db")
    { }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<Sub1>().HasRequired(x => x.Vender).WithOptional(x => x.Customer);
        modelBuilder.Entity<Sub1>().Map(x =>
            {
                x.ToTable("Sub1s");
                x.MapInheritedProperties();
            }).HasKey(x => x.BaseId);
        modelBuilder.Entity<Sub2>().Map(x =>
            {
                x.ToTable("Sub2s");
                x.MapInheritedProperties();
            }).HasKey(x => x.BaseId);
    }

    public DbSet<Base> People { get; set; }
    public DbSet<Sub2> Venders { get; set; }
    public DbSet<Sub1> Customers { get; set; }
}

然后我运行代码和EF创建表(Sub1s和Sub2s)。 但EF为表Sub1创建了一个附加的外键,它不使用Sub1的Key作为外键。它生成一个名为Vender_BaseId的额外FK ..

0 个答案:

没有答案