Entity Framework - Save ICollection Enumerations in Database

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 save the CustomerAtrributes property in the database, which will be easily restored 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 am using EntityFramework 6 to store my objects in a database using the CodeFirst approach.

EDIT: friend found a possible duplicate: ef 5 assembly of the codefirst enum code not created in the database . But, if you have different ideas or workarounds, post them,

+4
source share
1 answer

An enumeration is still a primitive type, in particular an integer. Just as your Costumer class cannot have an ICollection<int> that maps to something in the database, it cannot have a set of enumerations.

You need to create the CostumerType class and rename enum:

 public enum TypesOfCostumer //example name { VIP, FrequentCustomer, BadCustomer } public class CostumerType { public int Id { get; set; } public TypesOfCostumer Type {get; set;} } 
+1
source

All Articles