[英]Entity Framework - Save ICollection of Enumeration in database
I have a class with a property of type ICollection<Enum>
. 我有一个类
ICollection<Enum>
属性的类。
Say for example we have the following enumeration
: 比方说,我们有以下
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? 如何将属性
CustomerAtrributes
存储在数据库中以便以后轻松检索?
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. 编辑:我正在使用EntityFramework 6使用CodeFirst方法将我的对象存储在数据库中。
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. 编辑:朋友发现可能重复: ef 5 codefirst枚举集合未在数据库中生成 。但是,如果您有任何不同的想法或解决方法,请发布它们。
An enum is still a primitive type, in particular an integer. 枚举仍然是原始类型,特别是整数。 Just as your
Costumer
class can't have an ICollection<int>
that maps to something in the database, it can't have a collection of the enum. 正如您的
Costumer
类不能将ICollection<int>
映射到数据库中的某些内容一样,它也不能包含枚举的集合。
You have to create a CostumerType class and rename the enum: 您必须创建一个CostumerType类并重命名枚举:
public enum TypesOfCostumer //example name
{
VIP,
FrequentCustomer,
BadCustomer
}
public class CostumerType
{
public int Id { get; set; }
public TypesOfCostumer Type {get; set;}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.