How do you use the Code First technique to construct inheritance Table-Per-Type in Entity Framework 4.3?

ef-code-first entity-framework table-per-type

Question

This class hierarchy exists in my code:

[Table("A")]
public class A : IIdentification
{
    public int id { get; set; }
}

[Table("B")]
public class B : A
{
    //some properties here
    public Aid { get; set;}
    ForeignKey("Aid");
    public A A { get; set; }
}

[Table("C")]
public class C : B
{
    //some properties here 
    public Bid { get; set; }
    ForeignKey("Bid");
    public B B { get; set; }
}

[Table("D")]
public class D : C
{
    public Cid { get; set;}
    ForeignKey("Cid");
    public C C { get; set; }
}

What is the best method to create a single table with a foreign key for each class, if at all possible? Maybe someone could provide an example on how to do it?

Maybe this is trivial question but I've spent whole day, implemented many solutions described in topics and it still not work. So I'll be very grateful if someone could help me with that.

1
2
11/2/2015 7:09:41 PM

Accepted Answer

If you don't wish to model another relation, there are no available navigation properties to the parent object in mapped inheritance. Due to the fact that inheritance "is a" relation, B is A and FK "has a" connection.

For TPT inheritance, you have a wonderful walkthrough in this case.

5
3/5/2012 10:36:26 AM


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