2

I have this class hierarchy in 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; }
}

How can I make one table with a foreign key for each class or any other possible correct way? Maybe someone could post some sample how to achieve 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 Answer 1

5

In mapped inheritance you don't have accessible navigation property to parent entity unless you want to model another relation. Inheritance is "is a" relation so B is A while FK is "has a" relation.

Here you have a great walkthrough for TPT inheritance.

Sign up to request clarification or add additional context in comments.

5 Comments

Ladislav, thank you, I've seen that post early but now, after your answer I did it step-by-step like told in this post and get the same error:
sorry, mispressed enter instead of shift+enter... Error 3034: Two entities with different keys are mapped to the same row. Ensure these two mapping fragments do not map two groups of entities with overlapping keys to the same group of rows. I tried to specify all classes as abstract except D. Deleted all FK-stuff and in Dbset block write A, B & C.
I need to say that in situation: "B : A" it works fine, but when C tries to inherit from B and B from A this error occurs.
Ladislav, thank you for your advice and link. I've re-checked it more carefully today and have implemented on a clear project with the latest version of EF (4.3.1) and everything works fine!
Fyodor, Do you know what was the problem? I'm getting the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.