EF不想正确映射我的课程

时间:2016-12-15 12:27:09

标签: c# entity-framework

我有以下类Aggregate:

public class Aggregate
{
    public long? Id { get; set; }
    public A A { get; set; }
    public B B { get; set; }

    public virtual OuterClass Outer { get; set; }
}

public class A:C
{
}

public class B:C
{
}

public class C
{
    public bool Val1 { get; set; }
    public bool Val2 { get; set; }      
}

OuterClass已经添加到表中,Aggregate类具有1-1的OuterClass表。

我想将我的Aggregate类映射到两个不同的表:A和B,如下所示:

        modelBuilder.Entity<Aggregate>().HasRequired(x => x.Outer).WithRequiredDependent();
        modelBuilder.Entity<Aggregate>().Map(m =>
        {
            m.MapInheritedProperties();
            m.Properties(p => new { p.Id, p.A });
            m.ToTable("A");
        }).Map(m =>
        {
            m.MapInheritedProperties();
            m.Properties(p => new { p.Id, p.B });
            m.ToTable("B");
        });

然后我添加迁移并成为以下内容:

       CreateTable(
            "A",
            c => new
                {
                    Id = c.Decimal(nullable: false, precision: 19, scale: 0),
                    A_Val1 = c.Decimal(nullable: false, precision: 1, scale: 0),
                    A_Val2 = c.Decimal(nullable: false, precision: 1, scale: 0),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("OuterTable", t => t.Id)
            .Index(t => t.Id);         

        CreateTable(
            "B",
            c => new
                {
                    Id = c.Decimal(nullable: false, precision: 19, scale: 0),
                    B_Val1 = c.Decimal(nullable: false, precision: 1, scale: 0),
                    B_Val2 = c.Decimal(nullable: false, precision: 1, scale: 0),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("A", t => t.Id) /*!!!!!!!!!!!!!!!!!!!!!!!!!!*/
            .Index(t => t.Id);

为什么第二个表想要将FK添加到第一个表?如何告诉EF添加FK&#34; OuterTable&#34;代替?

0 个答案:

没有答案