lundi 26 octobre 2015

EF4 how to support inherited Entities, does EF5 support this?

Scenario:

I'm using EF4.

I have 3 entities in my EF context, Employee, HourlyEmployee inherit Employee, SalariedEmployee inherit Employee. Code first created one table and with a discriminator column.

Due to 3 of them are treated separately most of time, and for easy db manage, I thought split them to three tables by set entity mapping in OnModelCreating method. (Bad idea.)

Now whenever I load two different types of entities with same ID, EF complains with error "all objects in the entityset must have unique primary keys....", kind of like this issue: Entity framework and inheritance: NotSupportedException

I can only think of two solutions but don't know which one is doable, and if there is miracle work around for this issue? So I can keep 3 tables and inheritance.

  1. remove inheritance, 3 stand alone entities. So I can keep 3 tables.
  2. remove the entity mapping in OnModelCreating, so I end up with one table.
  3. .....??????

Also, does EF5 support these kind of inheritance and entity mapping to different table without discriminator column?

Thanks a lot for reading.

Aucun commentaire:

Enregistrer un commentaire