简体   繁体   中英

Entity Framework 6 multiple table to one foreign key relationship code first

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

在此输入图像描述

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

在此输入图像描述

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

在此输入图像描述

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM