Enum type mapping discriminator support
See original GitHub issueNpgsqls Enum Type Mapping is very nice. Unfortunately its not possible to use enums in the discriminator configuration.
For example the following code …
class SampleContext : DbContext
{
public SampleContext(DbContextOptions options)
: base(options)
{ }
static SampleContext()
=> NpgsqlConnection.GlobalTypeMapper.MapEnum<PersonType>();
public DbSet<Person> Persons { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasPostgresEnum<PersonType>();
modelBuilder.Entity<Person>()
.HasDiscriminator()
.HasValue<FactoryWorker>(PersonType.FactoryWorker)
.HasValue<Manager>(PersonType.Manager);
modelBuilder.Entity<Manager>();
modelBuilder.Entity<FactoryWorker>();
}
}
public abstract class Person
{
public int Id { get; set; }
public string Value { get; set; }
public Instant Timestamp { get; set; }
}
public class Manager : Person
{
public string Position { get; set; }
}
public class FactoryWorker : Person
{
public string FactoryName { get; set; }
}
public enum PersonType
{
Manager,
FactoryWorker
}
… yields the following exception:
Cannot set discriminator value ‘FactoryWorker’ for discriminator property ‘Discriminator’ because it is not assignable to property of type ‘System.String’.
It would be nice to use enums for discriminator columns.
Issue Analytics
- State:
- Created 4 years ago
- Comments:13 (6 by maintainers)
Top Results From Across the Web
Entity Framework 5 - Enum based Discriminator for derived ...
Supported types include byte, signed byte, bool, int16, int32, int64, and string. All I have to do was something like this: public enum...
Read more >Enums as type discriminator anti-pattern - Link Intersystems
In this blog I want to discuss the misuse of enums as type discriminators and show you why this kind of usage is...
Read more >javax.persistence.DiscriminatorType - JPA enum
Defines supported types of the discriminator column. Since: JPA 1.0. DiscriminatorType CHAR. Single character as the discriminator type.
Read more >Mapping Java enum to discriminator column
Hi, I have a table (Items) which is mapped to three different java classes using a discriminator column and subclasses in the Items.hbm.xml ......
Read more >Inheritance - EF Core
The actual details of how a type hierarchy is mapped are provider-dependent; this page describes inheritance support in the context of a ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Great! Good to know it worked out. Enums really are a sort of natural fit as discriminators, just keep in mind the limitations of altering them (https://github.com/npgsql/efcore.pg/issues/1308#issuecomment-599136265).
Thanks @roji for your help! Very much appreciated!
Adding a static constructor fixed it. This way the global type mapping gets also updated during design-time db context creation.