How to map the discriminator column to an enum when using a TPH inheritance in EF Core?
You can map a discriminator column to an enum type by first creating the enum and then mapping the TPH
inheritance by using the enum type as a generic parameter to the HasDiscriminator
method.
Here is an example:
language-csharppublic enum AnimalType
{
Cat,
Dog
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Animal>()
.HasDiscriminator<AnimalType>("DiscriminatorColumnName")
.HasValue<Cat>("Cat")
.HasValue<Dog>("Dog");
}