4

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.

Community
  • 1
  • 1

1 Answers1

1

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;}
}
Fabio
  • 11,892
  • 1
  • 25
  • 41