Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enums Stored as Strings #1381

Closed
motowilliams opened this issue Jan 9, 2015 · 12 comments
Closed

Enums Stored as Strings #1381

motowilliams opened this issue Jan 9, 2015 · 12 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@motowilliams
Copy link

Will EF7 have optional (or default) support for storing the enum string values instead of the numeric values in the database?

@rowanmiller
Copy link
Contributor

The default will still be to store the underlying type of the enum. Swapping to use the string will be covered by #242

@CharlesToniolo
Copy link

Is there any workaround to make it work with we have for now (RC1 or RC2)?
We are using automapper to translate these strings to enum (and enum to string), but doing this, almost of our queries have AsNoTracking and it is leading us in some troubles.

@divega
Copy link
Contributor

divega commented May 5, 2016

You could do the conversion in code in the entity and map the underlying string property using EF (you can make the property private and still map it) and leave the enum property unmapped (e.g. with [NotMapped]).

@CharlesToniolo
Copy link

That is exactly what i thought. Thanks!

@jeanmallet
Copy link

Worked fine for me. Thank you divega.

@DantaliaN00
Copy link

How about LINQ?

I say about something like

enum MyEnum {
  SomeEnum
}

class DbEntity {
  [Column("my_val")]
  public MyEnum MyDbVal {get;set;}
}

//in DbContext
DbSet<DbEentity> Items {get;set;}

//and somewhere
db.Items.Single(i => i.MyDbVal == MyEnum.SomeEnum)

I know, I can use Mapped string and NotMapped Enum, but it's realy ugly with linq.

I actualy want to see strings in equation and in DB for many reasons
Maybe EF has some properties/options/settings about that?

@divega
Copy link
Contributor

divega commented Aug 13, 2016

@DantaliaN00 server-side querying based on equality should be enabled for the majority of mappings by the type conversion feature described in #242. We will start working on that feature soon, although we are planning to ship it after 1.1.

@ajcvickers ajcvickers self-assigned this Oct 16, 2017
@ajcvickers ajcvickers added this to the 2.1.0 milestone Oct 16, 2017
@ajcvickers ajcvickers reopened this Oct 16, 2017
ajcvickers added a commit that referenced this issue Oct 16, 2017
Part of issues #242 and #1381

Instead of using special code for materialization and parameter creation, type mappings now add an appropriate type converter for enum types that converts two and from the underlying numeric value. (Special code is still present for providers that are not updated and do not create mappings with converters.)

This means that if the column is instead a string column, then a different converter can be used which converts to and from the string values of the enum. SQL Server and most other providers will get this for free when updating to 2.0 and adding support for value conversions. SQLite is the exception since it has weird type mapping.
ajcvickers added a commit that referenced this issue Oct 17, 2017
Part of issues #242 and #1381

Instead of using special code for materialization and parameter creation, type mappings now add an appropriate type converter for enum types that converts two and from the underlying numeric value. (Special code is still present for providers that are not updated and do not create mappings with converters.)

This means that if the column is instead a string column, then a different converter can be used which converts to and from the string values of the enum. SQL Server and most other providers will get this for free when updating to 2.0 and adding support for value conversions. SQLite is the exception since it has weird type mapping.
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 17, 2017
@ajcvickers
Copy link
Member

In 2.1, mapping an enum property to a string property will result in the enums being saved as strings in the database.

@roji
Copy link
Member

roji commented Oct 29, 2017

@ajcvickers, any chance that native database enums could be supported as well (i.e. #3620)? This would be great for PostgreSQL.

@ajcvickers
Copy link
Member

@roji Yes, but there are some more changes needed that have not yet been completed. They should be done relatively soon (depending on how much time I get to write code). The general idea is that:

  • If the provider's type mapper now says that it supports the enum natively, then EF will not attempt to convert it
  • If some other value converter has been supplied for the enum, then EF will use that
  • Otherwise, EF will attempt to use an enum-to-number or enum-to-string conversion depending on the column type

@roji
Copy link
Member

roji commented Oct 31, 2017

@ajcvickers wonderful, I'll be looking forward to implementing PostgreSQL support for this!

One quick question: if the user wants to override and store the enum as a number or string regardless of what the type mapper says, they'll still be able to do so via a value converter, right?

@ajcvickers
Copy link
Member

@roji Yep.

@ajcvickers ajcvickers modified the milestones: 2.1.0-preview1, 2.1.0 Nov 11, 2019
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

8 participants