December 28, 2012

The underlying type of CLR enumeration type does not match the underlying type of EDM enumeration type

Entity Framework has supported the enumeration type since its version 5. I followed tutorials at Enum Support - EF Designer and Entity Framework 5 + Enumerations => What’s not to love? to add the enum type in my model, however, I got 'The underlying type of CLR enumeration type does not match the underlying type of EDM enumeration type' in Test Explorer after I ran one of my unit tests against a SQL Server 2008 database.
The column in my database table is of type 'tinyint', which will map to 'byte' in .NET. And here's what my enum type looks like.
    public enum ForgotPasswordStatus
    {
        Pending = 0,
        Verified = 1
    }

Checking another article at USING EXISITING ENUM TYPES IN ENTITY FRAMEWORK 5, I found the reason I got the error was because I did not explicitly define the underlying type of my enum ForgotPasswordStatus. After adding the underlying type to my enum as follows, my test passes now.
    public enum ForgotPasswordStatus : byte
    {
        Pending = 0,
        Verified = 1
    }

No comments: