实体框架 - 在数据库中保存枚举的ICollection

时间:2015-08-18 12:41:27

标签: c# entity-framework collections enums

我有一个类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 collection not generated in database。但是,如果您有任何不同的想法或解决方法,请发布。

1 个答案:

答案 0 :(得分:1)

枚举仍然是原始类型,特别是整数。正如您的Costumer班级无法将ICollection<int>映射到数据库中的某些内容一样,它也无法收集枚举。

您必须创建一个CostumerType类并重命名枚举:

public enum TypesOfCostumer //example name
{
    VIP,
    FrequentCustomer,
    BadCustomer
}

public class CostumerType
{
    public int Id { get; set; }
    public TypesOfCostumer Type {get; set;}
}