2

Ok, Entity Framework 6.1.3, .Net Framework 4.5, and here is example code.

Suppose, I have an entity class like this:

public enum IdiocyLevel : uint { ExtremlyDumb, MaybeNotDumb, SmartEnough }
public enum SpreadLevel : byte { NoBodyUseIt, CommonUse, DeFactoStandart }

public class FrameworkReview
{
    public int Id { get; set; }
    public String FrameworkName { get; set; }
    public IdiocyLevel Idiocy { get; set; }
    public SpreadLevel Spread { get; set; }

    public override string ToString() { return String.Format("{0}:{1} - {2}, {3}", Id, FrameworkName, Spread, Idiocy); }
}

Very simple. Id key, string and two enum values. First enum is backed as uint, second as byte.

I have a DbContext class like this:

public class DatabaseContext : DbContext
{
    public DbSet<FrameworkReview> Frameworks { get; set; }

    public DatabaseContext()
        : base("Data Source=(localdb)\\v11.0; Integrated Security=True; " +
               "AttachDbFilename=" + AppDomain.CurrentDomain.BaseDirectory + "enum.mdf")
    {
    }
}

Now, if I use this classes, my first enum is always stored as default value:

class Program
{
    static void Main(string[] args)
    {
        FrameworkReview originalReview = new FrameworkReview()
        {
            FrameworkName = "EntityFramework",
            Idiocy = IdiocyLevel.SmartEnough,
            Spread = SpreadLevel.DeFactoStandart
        };
        Console.WriteLine(originalReview);

        int storedReviewId = 0;

        using (DatabaseContext dbContext = new DatabaseContext())
        {
            FrameworkReview storedReview = dbContext.Frameworks.Add(originalReview);
            dbContext.SaveChanges();

            Console.WriteLine(storedReview);
            storedReviewId = storedReview.Id;
        }

        using (DatabaseContext dbContext = new DatabaseContext())
        {
            FrameworkReview readedReview = dbContext.Frameworks.Find(storedReviewId);
            Console.WriteLine(readedReview);
        }

        Console.ReadKey(true);
    }
}

The output will be

0:EntityFramework - DeFactoStandart, SmartEnough
1:EntityFramework - DeFactoStandart, SmartEnough
1:EntityFramework - DeFactoStandart, ExtremlyDumb

Why? EF doesn't allow me to store uint values (which is default for C# enums)? Is this stated somewhere in documentation?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
vasily.sib
  • 3,871
  • 2
  • 23
  • 26

1 Answers1

4

Unsigned types are not supported by Entity Framework.

Read more about it here: How to use unsigned int / long types with Entity Framework?

Community
  • 1
  • 1
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
  • Why answering and not marking the question as duplicate of the linked post? – Ivan Stoev Mar 03 '17 at 12:12
  • The posted question and its answers are not about enums. The reason of problem is common - EF6 does not support unsigned types, and the answer is the same. However, the question is not about the same thing, i.e. it is not a duplicate. Someone can stumble upon the same problem and google for "uint enum ef6 not working", and he will find this question, but won't find the linked one. I just felt something like "naah it isn't a dupe" when I decided to not mark as dupe. I may be wrong, of course :) – Yeldar Kurmangaliyev Mar 03 '17 at 18:09
  • Got it, fair enough :) – Ivan Stoev Mar 03 '17 at 18:10
  • Ok, I google "ef unsigned types" and found that they mark this as "Wont fix": https://entityframework.codeplex.com/workitem/1489 – vasily.sib Mar 04 '17 at 05:52