How to map a related table to an inherited entity (table-per-hierarchy)?

By : Mariusz

i've got this tables in my database.

alt text

With Entity Framework i would like to have something like that:

alt text

I've used table-per-hierarchy inheritance, so i added a new entity for the premium user and map it to the user entity with conditions to IsPremiumUser property. I would like to have the PremiumAccount table associate to the PremiumUser entity, but it won't work like i did it :-/ I've got this error message:

Error 3021: Problem in Mapping Fragment starting at line 113: Each of the following columns in table PremiumAccount is mapped to multiple conceptual side properties: PremiumAccount.UserID is mapped to

Is there any solution to my problem? Can I associate the PremiumAccount entity to an inherited entity? Is there another way to do this?

By : Mariusz


I think that the subtype's PK field should be removed (as it inherits it from its supertype).

That said, this isn't a good inheritance candidate. Entity inheritance should only be performed if the type can't change for an entity. This is more a 'role' candidate, so a real 1:1 relationship. The thing is that User might lose / gain the Premium role on a regular basis, but you can't change a type of an existing entity instance (== table rows) even if it seems like you can by simply altering the data in the database. Inheritance mapping is about strict rules, you can't bend them, as inheritance in entities is about interpreting data in a database, as relational models don't know the concept of inheritance (although some databases 'cheat' by allowing inheritance in DDL SQL like postgresql)

This video can help you solving your question :)
By: admin