In a usual TPH implementation, a table has a "Discriminator" column used by EF to decide which class to instantiate. I have a db in which a the discriminator references another table, instead, containing a list of entity types and their relative codes. How to configure the dbContext, so that the discriminator is now a field in the referenced table?
To simplify things, let's build a simple example: a table "Pets" containing a list of pets, implemented in concrete classes "Dog" and "Cat", both derived from "Pet". Class Cat has a field "Mews", indicating the number of mews; class Dog has a field named "Woovs".
Typical implementation:
The Pets table has an identity column "Id" (primary key), a "Discriminator" indicating the type of concrete class, a Mews and a Woovs fields.
The dbContext is instructed to instantiate the proper concrete class like this.
modelBuilder
.Entity<Pet>()
.HasDiscriminator(c => c.Discriminator)
.HasValue<Cat>("CAT")
.HasValue<Dog>("DOG")
;
My requirements:
Another table exists, "PetTypes", with an identity column "Id" (primary key) and a "Code" column, with unique constraint. The Pets table has no Discriminator column; it has a PetTypeId column, instead, referencing the PetTypes table (foreign key to PetTypes' Id column).
I would like to instruct the modelBuilder to use the PetTypes' Code property as discriminator; something like:
modelBuilder
.Entity<Pet>()
.HasDiscriminator(c => c.PetType.Code)
.HasValue<Cat>("CAT")
.HasValue<Dog>("DOG")
;
or:
modelBuilder
.Entity<Pet>()
.HasDiscriminator(c => c.PetType)
.HasValue<Cat>(new PetType(Code = "CAT"))
.HasValue<Dog>(new PetType(Code = "DOG"))
;
or some other way, so that the Pets class discriminator is agnostic ot the PetTypeId, but only deals with the PetType's code.
Is there any way to do the job?