8

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);
        })
John Zabroski
  • 2,212
  • 2
  • 28
  • 54
panJapa
  • 113
  • 1
  • 2
  • 6
  • Try explicitly saying the enum is an int `public enum LimitMode : int` and giving the members of the enum a value (`Max = 0, ...`) – Alexander Derck Feb 09 '16 at 18:24
  • I have tried this. Unfortunately it does not work. Still getting invalid cast exception :/ – panJapa Feb 09 '16 at 18:53
  • Strange, in EF6 it works perfectly. EF7 isn't fully released yet so it might still be a bug, or you have values in your database that aren't connected to any of your enums. If your enum values are 1-3 and there is 4 stored in your database I suppose you would get that error. – Alexander Derck Feb 09 '16 at 18:57
  • https://github.com/aspnet/EntityFramework/issues/3620 –  Feb 09 '16 at 20:23

2 Answers2

6

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; }
}
schnitty
  • 416
  • 4
  • 12
  • Don't post same answer under different question. If you think question's are same flag question as duplicate.1. https://stackoverflow.com/questions/47721246/ef-core-2-0-enums-stored-as-string/49913196#49913196 2. https://stackoverflow.com/questions/44262314/how-can-i-make-ef-core-database-first-use-enums/49913219#49913219 3. https://stackoverflow.com/questions/35298829/does-ef7-support-enums/49913063#49913063 – 4b0 Apr 19 '18 at 05:57
  • 1
    Ok, sorry about that. I have flagged the other two as duplicates of this one as this is the oldest. – schnitty Apr 19 '18 at 06:08
1

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
}
Sergey Barskiy
  • 1,761
  • 2
  • 15
  • 17