Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does EF7 (EFCore) support enums?

I have problem with EF7 RC1 (EFCore). I am unable to work with enums in my model. I can save enum property. The value is casted to int. My problem is that during data reading i get invalid cast exception.

  1. Does EF7 support enum properties ?
  2. How can i configure it with fluent api ?

Thanks

EDIT:

enum:

  public enum LimitMode 
    {
        Max,
        Min,
        MaxAndMin,
    }

Model:

  public class SomeModel 
    {
    (..)
    public LimitMode LimitMode {get; set;}
    }

ModelBuilder for SomeModel:

        modelBuilder.Entity<SomeModel>(entity => {
            (...)
            entity.Property(p => p.LimitMode);
        })
like image 414
panJapa Avatar asked Feb 09 '16 17:02

panJapa


People also ask

Does EF Core support enums?

The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong. Show activity on this post. Currently, EF core does not support enums. Code like this: public class MyDbContext : DbContext { protected override void OnModelCreating (ModelBuilder builder) { builder.Entity<StateEnum> (e => {...});

How to convert an enum to a different type in EF?

This enum is already defined in code: Show activity on this post. Starting with Entity Framework Core 2.1, EF supports Value Conversions to specifically address scenarios where a property needs to be mapped to a different type for storage. Specifically for Enums, you can use the provided EnumToStringConverter or EnumToNumberConverter.

What are value conversions in EF Core?

Value Conversions are new in EF Core 2.1, they provide an easy way of converting from entity property types into database column types and vice versa. I’ve gone into more detail in a previous post.

What are the types of enumerated values in Entity Framework?

The default data type of enumerated values is Integer, which starts from zero index, and the value gets increased by one. In Entity Framework, it can have the following types: Entity Framework Core supports both approaches i.e. Code First and Database First. It is a light-weighted and cross platform technology to access the database.


2 Answers

Value converters are now supported in EntityFrameworkCore 2.1 This allows you to treat Enums as strings in your database and have them correctly convert to Enums in your model.

You do this with a value converter. You can create your own but EF Core 2.1 comes with some pre-defined value converters out of the box. One of these is the EnumToString value converter.

So given the following:

public class Rider
{
    public int Id { get; set; }
    public EquineBeast Mount { get; set; }
}

public enum EquineBeast
{
    Donkey,
    Mule,
    Horse,
    Unicorn
}

Use the default converter like this:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder
        .Entity<Rider>()
        .Property(e => e.Mount)
        .HasConversion<string>();
}

Or if you prefer using attributes on your model classes like this:

public class Rider
{
    public int Id { get; set; }

    [Column(TypeName = "nvarchar(24)")]
    public EquineBeast Mount { get; set; }
}
like image 150
schnitty Avatar answered Oct 06 '22 23:10

schnitty


This worked for me. I am using "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final" in project.json. I had to run ef migrations database update as part of pushing the model.

public class Person
{
    public int PersonId { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public int PersonTypeId { get; set; }
    public PersonType PersonType { get; set; }
    public ActiveType ActiveType { get; set; }
}

public enum ActiveType
{
    Active = 0,
    Inactive = 1
}
like image 28
Sergey Barskiy Avatar answered Oct 06 '22 22:10

Sergey Barskiy