Entity Framework 6 multiple tables for one foreign key interaction code

I am wondering if anyone can advise me how to do the following code usage first in EF6

enter image description here

If I add table_3 to the list in table_1 and table_2 in my entities. EF automatically creates a foreign key column for both tables in table_3 instead of recognizing that they are of the same type.

My model classes are defined 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 below

enter image description here

Edit

Just to let anyone with the same problems know

Now I solved this by changing the IParent interface to an abstract class, my classes now look like this

[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 table layout

enter image description here

this will work, although it would be better if the original could be executed.

+4
2

, . table_3 : Table_1, Table_2, EF , public virtual parentbase {get;set;}. . , .

. Children {get; ; } parentbase, .

, .

+1

: 1 2 Table_1 Table_2 Table_3 :

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);

, .

+1

All Articles