ef 5 codefirst enum collection not generated in database

后端 未结 3 1447
花落未央
花落未央 2020-12-20 23:34

I am using EF5 and .NET 4.5. I have one particular class that is being generated incorrectly in the database. Although it is somewhat more complicated in my website, I\'ll s

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-20 23:51

    Based on the answer by Gert Amold you could create a nice wrapper class by using the implicit operator:

    public class RoleWrapper
    {
        public int Id { get; set; }
    
        // Role is any Enum
        public Role Role { get; set; }
    
        public static implicit operator Role(RoleWrapper val)
        {
            return val.Role;
        }
        public static implicit operator RoleWrapper(Role val)
        {
            return new RoleWrapper() { Role = val };
        }
    }
    

    This way you can use the collection with the primitiv Enum type Role:

    myUser.Roles.Add(Role.WhateverRole);
    Role firstRole = myUser.Roles[0];
    

提交回复
热议问题