dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.65k stars 3.15k forks source link

Support for database enums #3620

Closed roji closed 6 years ago

roji commented 8 years ago

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?

dlogik commented 8 years ago

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

ryancole commented 8 years ago

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?

rowanmiller commented 8 years ago

We will look at it again post v1.0.0

vankooch commented 8 years ago

Would be great to get this working.

stevenxi commented 8 years ago

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.

karldodd commented 7 years ago

Looking forward to this.

kernel-io commented 7 years ago

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

ian-luca commented 6 years ago

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!?

ajcvickers commented 6 years ago

@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

markusschaber commented 6 years ago

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

vankooch commented 6 years ago

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

roji commented 6 years ago

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.

ajcvickers commented 6 years ago

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

roji commented 6 years ago

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