c# - Entity Framework - 在数据库中保存枚举的 ICollection

标签 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 方法将我的对象存储在数据库中。

编辑:一位 friend 发现了一个可能的副本:ef 5 codefirst enum collection not generated in database .但是,如果您有任何不同的想法或解决方法,请将它们张贴出来。

最佳答案

枚举仍然是原始类型,尤其是整数。正如你的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;}
}

关于c# - Entity Framework - 在数据库中保存枚举的 ICollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32072732/

相关文章:

c# - C# 中的抽象类与静态类

linq - 学习 LinqToSql 还是坚持使用 ADO.NET?

c# - 对 IQueryable OfType<> 的反射(reflection)

Java 集合排序 : DEBUG/VERBOSE the RootCause of "Comparison method violates its general contract:" errors

c# - ASPxCombobox,允许用户输入和下拉选择

c# - 线程顺序执行?

c# - 我可以在 Entity Framework 6 中的关系中为属性定义过滤器吗?

java - 推断类型不是 Comparable 泛型类型的有效替代品

android - stream() 方法在 android 中不起作用

c# - Microsoft.AspNetCore.Cryptography.Internal.dll : 'Unable to load DLL ' kernel32. dll'