0

example simplified class structure

Hi All

I am havinga problem with Entity framework, basically as you can see in the above diagram class A is an abstract class that has two classes that inherit from it B & C.

Each Class has an ID which is not a requirement but was part of the relational structure of the Database design.

I am trying to generate using the EF tools in VS 2008 a model of my Database and then model in the inheritance. I get the model generated fine but the Foreign Keys are still present in the model diagram. I delete those and replace them with the correct inheritance, I also set the Entity Set Names to be the same.

and this is where my problems begin.

The classes do not seem to be working correctly the associations are not working?

Was the removal of the FK correct or should I have left it in place and used the inheritance as well?

I need classes B and C to inherit A in my business layer.

The model doesn't seem correct and I would be gtreatful for any help in making this model work.

6
  • is it per table or per type inheritance? is it database first scenario? Commented Mar 21, 2012 at 13:32
  • 1
    The "id" fields are primary keys in the tables, or is it a PK in A and FK in B & C? Commented Mar 21, 2012 at 13:34
  • @DenisAgarev I believe Table-per-type as I have a Table for A, B & C Commented Mar 21, 2012 at 13:34
  • @CodingGorilla they are primary keys with a second field which (my rushed) diagram does not show which is a FK to the PK of A -- So A, B & C have PK of ID B, C have a FK to A for ID Commented Mar 21, 2012 at 13:36
  • "do not seem to be working correctly" -> what does that mean? Commented Mar 21, 2012 at 13:39

2 Answers 2

2

You should not have a (separate) FK to the PK of A.

Inheritance expects the B.Id to be PK and an FK to A.Id.

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

Comments

1

I think the problem is that you have a separate PK and FK that relates your tables. EF expects that the child tables have a single "id" column, in the child tables, that is both the PK and the FK.

Here is a snapshot of an example of a database that I have (this was generated by EF, but you see what I'm talking about):

Example

You'll see here that Jobs is the base and the other two derive from Jobs. You'll see that the Id field in Jobs is the PK, then in the other two tables the Id field is both a PK as well as a FK back to Jobs.

Like I said, this model was generated by EF (Model first), but I'm pretty sure this is how EF wants to see things, so I don't think your database will fit into EF's inheritance structure.

Comments

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.