实体框架 - 在数据库中保存枚举的ICollection [英] Entity Framework - Save ICollection of Enumeration in database
问题描述
我有一个类的属性类型 ICollection< Enum>
。
例如我们有以下枚举
:
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 |姓氏CustomerType |
1 | Bob |史密斯| VIP |
1 | Bob |史密斯|常见客户|
2 |迈克|约旦|坏客户|
编辑:我使用EntityFramework 6使用CodeFirst方法将我的对象存储在数据库中。 / p>
编辑:朋友发现了一个可能的重复: ef 5 codefirst枚举集合不是在数据库中生成。但是,如果您有任何不同的想法或解决方法,请发布。
解决方案枚举仍然是原始类型,特别是整数。正如你的
Costumer
类不能有一个映射到数据库中的东西的ICollection< int>
没有枚举的集合。
您必须创建一个CostumerType类并重命名枚举:
public enum TypesOfCostumer // example name
{
VIP,
FrequentCustomer,
BadCustomer
}
public class CostumerType
{
public int Id {get;组; }
public TypesOfCostumer Type {get; set;}
}
I have a class with a property of type
ICollection<Enum>
.Say for example we have the following
enumeration
:public enum CustomerType { VIP, FrequentCustomer, BadCustomer }
We also have the following class:
public class Customer { public int Id { get;set; } public string FirstName { get;set; } public string LastName { get;set; } public ICollection<CustomerType> CustomerAtrributes { get;set; } }
How can I store the property
CustomerAtrributes
in the database to be easily retrieved later?For example I'm looking for something like the following:
CustomerId | FirstName | LastName | CustomerType | 1 | Bob | Smith | VIP | 1 | Bob | Smith | FrequentCustomer| 2 | Mike | Jordan | BadCustomer |
EDIT: I'm using EntityFramework 6 to store my objects in the database using the CodeFirst approach.
EDIT: A friend found a possible duplicate : ef 5 codefirst enum collection not generated in database .But please, if you have any different ideas or workarounds, post them.
解决方案An enum is still a primitive type, in particular an integer. Just as your
Costumer
class can't have anICollection<int>
that maps to something in the database, it can't have a collection of the enum.You have to create a CostumerType class and rename the enum:
public enum TypesOfCostumer //example name { VIP, FrequentCustomer, BadCustomer } public class CostumerType { public int Id { get; set; } public TypesOfCostumer Type {get; set;} }
这篇关于实体框架 - 在数据库中保存枚举的ICollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!