0

我有一个像这样的 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的键作为外键..它生成一个名为Vender_BaseId的额外FK..

4

0 回答 0