Efcore: Support for database enums

Created on 31 Oct 2015  路  14Comments  路  Source: dotnet/efcore

PostgreSQL allows defining enums as first-class database types, which can then be used in regular columns. Npgsql exposes this by allowing users to define a mapping between a PostgreSQL enum and a CLR enum.

EF7's type mapping now allows arbitrary types to be mapped to supported database types, this works very well with PostgreSQL types - types such as macaddr are successfully mapped in EF7 to .NET PhysicalAddress. However, when trying to add enum mapping support I ran into trouble - it seems that in various areas of the EF7 core enums are unwrapped and treated as their underlying numeric type instead (e.g. int).

I'm guessing this is to allow enums to be saved to numeric columns for databases which don't support enums natively (e.g. SqlServer). Am I understanding the situation correctly, is database enum support currently impossible in EF7?

closed-fixed type-enhancement

Most helpful comment

@CoffeeDreamLabs I believe that the updates to the type mapper in 2.1 should allow the Postgres driver to support this starting in 2.1. /cc @roji

All 14 comments

Hi, would it be possible to give some pointers where this unwrapping behavior for Enums occurs in the EntityFramework code? I would be interested in forking and trying to get something working (at least hacking together a solution if possible) Thanks

I see this was removed from a certain priority label awhile back. Is this still on any radar anywhere, or has this been shelved for now?

We will look at it again post v1.0.0

Would be great to get this working.

How I get it work around is creating standard structured tables for each Enum.
And sync name/id when seeding / upgrading db.

So this would work no matter if db supports enum, the column in db is still number, but there's the foreign key that link to a proper table for the enum.

And with attribute on enum fields, you could even add description to it.

Most of work is done by generic code.

Looking forward to this.

Also looking forward to this, right now I'm also doing what @stevenxi does.

Any news on this matter?
As stated by @rowanmiller above this will be addressed post v1.0.0
Just want to know if we can expect some changes on this topic in the near future!?

@CoffeeDreamLabs I believe that the updates to the type mapper in 2.1 should allow the Postgres driver to support this starting in 2.1. /cc @roji

Is there any way to verify this? Is the ball on the npgsql side now?

I think the ball is on the EF team right now, as the npgsql team is waiting for a fix

Thanks for this work @ajcvickers, I'll take a look as soon as I can for preview2 and will post back here if I run into any further trouble.

For anyone interested in PostgreSQL native enums, please follow progress in https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/27.

@roji Wanted to let you know that @anpete uncovered an issue where enum literals in queries are being sent to the provider as literals of the underlying numeric type because of the expression tree created by the compiler. But we think it can be fixed.

Thanks for the heads-up, I'll work on non-literal functionality until this is resolved, is there an issue tracking this (or maybe just reopen this)?

Was this page helpful?
0 / 5 - 0 ratings