1

Hi I need to save a single enum in my context, I tryed this:

  public class Context : DbContext
  {
      public Context() : base("name=Context"){}

      MyEnum MyEnum  { get; set; }
      public DbSet<Object1> Objects{ get; set; }

}

, when I modify MyEnum it changes its value but after saving changes when I want to use it goes back to the enum of value 0. Heres my modification code:

 MyEnum example = MyEnum.Value1;
 using (var ctx = new Context())
        {
            ctx.CriterioPlanificacion = example;
            ctx.SaveChanges();
        }

3 Answers3

0

Enum default type is an int. It is normal that the value is 0 because it is the first element. If you want to put a description for your elements you can use the description attribute and retrieve it as shown here.

Community
  • 1
  • 1
AlexB
  • 3,518
  • 4
  • 29
  • 46
0

So I found this old question I made, so i tested it. The migrations after adding a enum to the DbContext are empty, so EF doesnt saves the ints in a DbContext.

Microsoft documentation recommends only using DbSets that only exposes DbSets with collections of TEntities in it

0

In my case, the problem was that the Enum field had property "StoreGeneratedPattern" with value = "Computed". After changing to 'None' - everything started working correctly.

reesly
  • 1
  • If you have a question just use Ask Question or provide a good solution for the user – yass Jul 02 '17 at 20:19