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

c# ef-code-first entity-framework sql

Question

I was hoping someone could give me some advice on how to complete the following utilizing code first in EF6.

enter image description here

In my entities, if I add Table 3 as a List after Table 1 and Table 2. EF doesn't recognize that both tables in Table 3 are of the same type; instead, it creates a foreign key field for both tables automatically.

The following describes my model classes.

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 produces the following.

enter image description here

Edit

Just to let anyone experiencing similar issues know

The IParent interface has now been changed to an abstract class, and my classes now have the following structure.

[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 configuration of

enter image description here

Although it would have been good if the original could have been met, this will work.

1
4
12/13/2013 12:04:37 PM

Accepted Answer

I experienced the same issue and started off using abstract classes rather than interfaces. My table 3 had two navigation properties: a public virtual Table 1 and a public virtual Table 2, and EF had recently added these new foreign key columns. This was a problem, so I combined the two navigation properties into a single navigation property.public virtual parentbase {get;set;}. Then it began to work. Hope this was useful.

Side Note: Since it was previously the case in your previous example, I'd advise adding the virtual keyword on public List Children to the parentbase class.

I ran across this problem also, so I appreciate you mentioning this.

1
5/15/2014 11:10:43 AM

Popular Answer

You can also create a 1 to many link between the following things: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);

If you need any additional clarity, please let me know.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow