实体框架-在数据库中保存枚举集合



我有一个类型为ICollection<Enum>属性的类。

例如,我们有以下enumeration:

public enum CustomerType
{
    VIP,
    FrequentCustomer,
    BadCustomer
}

我们还有下面的类:

public class Customer
{
    public int Id { get;set; } 
    public string FirstName { get;set; } 
    public string LastName { get;set; } 
    public ICollection<CustomerType> CustomerAtrributes { get;set; }
}

我如何将属性CustomerAtrributes存储在数据库中以便以后轻松检索?

例如,我正在寻找如下内容:

CustomerId | FirstName | LastName | CustomerType    |  
1          | Bob       | Smith    | VIP             |  
1          | Bob       | Smith    | FrequentCustomer|  
2          | Mike      | Jordan   | BadCustomer     |  

编辑:我使用EntityFramework 6使用CodeFirst方法在数据库中存储我的对象。

编辑:一个朋友发现一个可能的重复:ef 5 codefirst枚举集合没有在数据库中生成。但是请,如果你有任何不同的想法或解决方法,发布它们。

enum仍然是基本类型,特别是整数类型。就像你的Costumer类不能有一个ICollection<int>映射到数据库中的东西,它不能有枚举的集合。

你必须创建一个CostumerType类并重命名enum:

public enum TypesOfCostumer //example name
{
    VIP,
    FrequentCustomer,
    BadCustomer
}
public class CostumerType
{
    public int Id { get; set; }
    public TypesOfCostumer Type {get; set;}
}

最新更新