Mortalus
Mortalus

Reputation: 10712

auto generating Inheritance Mapping mvc DB first

Is it possible to define Inheritance Mapping and a Discriminator Property using a BD first approach.

I use the EDMX diagram to define the mapping of tables to object. I have a DomainEntity Table that contains all my domain entities and they are descriminated by TypeID that is mapped to a DomainEntityTypes table.

If i had full control i would design the mapping this way:

  1. Define an abstarct class DomainEntity
  2. Inherit from the DomainEntity calss to creat concrete entities
  3. Use the Inheritance Mapping Annotation and the IsDiscriminator Annotation over the TypeID
  4. Use an enum to define all possible types (and i wonder what the types table is for from that point)

Could all this be defined in the EDMX file somehow ? or do i need to stop using auto generation and continue with manual mappings?

Upvotes: 0

Views: 746

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364389

There is no "auto-generation" of inheritance mapping. When you use database first you simply load tables to your model and it will create entities with relations. Now if you want to have inheritance you must modify the mapping from the designer. Here is a nice description how to set up TPH inheritance in the designer.

  1. You can define base entity for your inheritance hierarchy as abstract. It is property of the entity in the diagram.
  2. You must inherit a new entity for all types you want to use and correctly set up its discriminator value in the mapping.
  3. There are no real annotations - inheritance is a construct available in the ToolBox and discriminator is a condition defined in the mapping.
  4. You will have no enum. Discriminator column will even not be available in your entities because it is already used to define mapping to correct type (there is limitation that each column can be used only once in the mapping so you can use it either for property or for discriminator but not for both). You will have finite set of entities to describe your inheritance hierarchy instead of enum.

Upvotes: 2

Related Questions