Entity Framework 6 multiple table to one foreign k

2019-04-09 11:41发布

I am wondering if anyone could advise me on how to accomplish the below using code first in EF6

enter image description here

If I add the Table_3 as a List on to Table_1 & Table_2 in my entities. EF automatically generates a foreign key column for both tables in Table_3 instead of recognizing that they are of the same type.

My model classes are set as follows.

public interface IParent
{
    int ID { get; set; }
    List<Table_3> Children { get; set; }
}

public class Table_1 : IParent
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual List<Table_3> Children { get; set; }
}

public class Table_2 : IParent
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual List<Table_3> Children { get; set; }
}

public class Table_3
{
    [Key]
    public int ID { get; set; }
    public int ParentID { get; set; }
    [ForeignKey("ParentID")]
    public virtual IParent Parent { get; set; }
}

EF code first generates the below

enter image description here

Edit

Just to let anyone having the same problems know

I have now resolved this by changing the IParent interface to an abstract class my classes now look like the following

[Table("ParentBase")]
public abstract class ParentBase
{
    [Key]
    public int ID { get; set; }
    public List<Table_3> Children { get; set; }
}
[Table("Table_1")]
public class Table_1 : ParentBase
{
    public string Name { get; set; }
}
[Table("Table_2")]
public class Table_2 : ParentBase
{
    public string Name { get; set; }
}
[Table("Table_3")]
public class Table_3
{
    [Key]
    public int ID { get; set; }
    public int ParentID { get; set; }
    [ForeignKey("ParentID")]
    public virtual ParentBase Parent { get; set; }
}

with a table arrangement of

enter image description here

this will work although it would have been nicer if the original could have been met.

2条回答
啃猪蹄的小仙女
2楼-- · 2019-04-09 11:56

You can also do like the following where you make a 1 to many relationship between Table_1 and Table_2 with Table_3 respectively:

modelBuilder.Entity<Table_3>().HasOptional(/*Nav Prop*/).WithMany(m => m.Table_3s).HasForeignKey(f => f.ParentId).WillCascadeOnDelete(false);
modelBuilder.Entity<Table_3>().HasOptional(/*Nav Prop*/).WithMany(m => m.Table_3s).HasForeignKey(f => f.ParentId).WillCascadeOnDelete(false);

Let me know if anymore clarification is required.

查看更多
聊天终结者
3楼-- · 2019-04-09 11:58

I had this problem too, and I used abstract class instead of interface from the beginning. The problem for mine was my table_3 have two navigation properties: one is public virtual Table_1, another is public virtual Table_2, and then EF just provisioned these extra foreign key columns, I merged the two navigation properties into one to public virtual parentbase {get;set;}. And then it worked. Hope this helps.

Side Note,Would suggest to add virtual keyword on public List Children { get; set; } in parentbase class, because in your previous example , it was already like that.

Thanks for posting this, i came across this issue too.

查看更多
登录 后发表回答