我有一个属性类型为 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/