Will EF7 have optional (or default) support for storing the enum string values instead of the numeric values in the database?
The default will still be to store the underlying type of the enum. Swapping to use the string will be covered by https://github.com/aspnet/EntityFramework/issues/242
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.
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]
).
That is exactly what i thought. Thanks!
Worked fine for me. Thank you divega.
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?
@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.
In 2.1, mapping an enum property to a string property will result in the enums being saved as strings in the database.
@ajcvickers, any chance that native database enums could be supported as well (i.e. #3620)? This would be great for PostgreSQL.
@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:
@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?
@roji Yep.
Most helpful comment
In 2.1, mapping an enum property to a string property will result in the enums being saved as strings in the database.