实体框架 - 在数据库中保存枚举的ICollection [英] Entity Framework - Save ICollection of Enumeration in database

查看:142
本文介绍了实体框架 - 在数据库中保存枚举的ICollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个属性类型为 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 <数据库中的code>可以在以后轻松检索?



例如,我正在寻找类似以下内容:

  CustomerId |名字|姓氏| CustomerType | 
1 |鲍勃|史密斯VIP |
1 |鲍勃|史密斯FrequentCustomer |
2 |迈克|约旦| BadCustomer |

编辑:我使用EntityFramework 6将我的对象存储在数据库中,使用CodeFirst方法。 / p>

编辑:朋友发现可能的重复: ef 5在数据库中未生成的代码第一个枚举集合。但是,如果您有任何不同的想法或解决方法,请发布。

解决方案

枚举仍然是原始类型,特别是整数。就像你的 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 an ICollection<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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆