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?

2
  • 1
    The default underlying type of C# enums is int.
    – Ivan Stoev
    Mar 3, 2017 at 12:10
  • Yap, default underlying type it's int. My bad.
    – vasily.sib
    Mar 4, 2017 at 5:35

1 Answer 1

4

Unsigned types are not supported by Entity Framework.

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

4
  • Why answering and not marking the question as duplicate of the linked post?
    – Ivan Stoev
    Mar 3, 2017 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 :) Mar 3, 2017 at 18:09
  • Got it, fair enough :)
    – Ivan Stoev
    Mar 3, 2017 at 18:10
  • Ok, I google "ef unsigned types" and found that they mark this as "Wont fix": entityframework.codeplex.com/workitem/1489
    – vasily.sib
    Mar 4, 2017 at 5:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.