question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enum type mapping discriminator support

See original GitHub issue

Npgsqls 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:closed
  • Created 4 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
rojicommented, Mar 16, 2020

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).

1reaction
davidrothcommented, Mar 16, 2020

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.

static BlogContext()
{
    NpgsqlConnection.GlobalTypeMapper.MapEnum<PersonType>();
    NpgsqlConnection.GlobalTypeMapper.MapEnum<UserType>();
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found