EF Core Tools

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-csharp
public enum AnimalType { Cat, Dog }

protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Animal>() .HasDiscriminator<AnimalType>("DiscriminatorColumnName") .HasValue<Cat>("Cat") .HasValue<Dog>("Dog"); }

This field is required
A text with a minimum of 10 characters is required
Answer generated by AI may produce inaccurate information about code, people, facts, and more.