Ef 5 codefirst recoding collection not generated in database

I am using EF5 and .NET 4.5. I have one specific class that is not created correctly in the database. Although this is somewhat more complicated on my website, I will simplify;

namespace Store.Enities
{
    public enum Role
    { Manager, Clerk }

    public class User
    {
        public int Id {get; set;}
        public ICollection<Role> Roles {get; set;}
    }

    public class StoreContext : DbContext
    {
        public DbSet<User> Users {get; set;}

        public StoreContext()
        {
            Database.SetIntializer(new DropCreateDatabaseIfModelChanges<StoreContext>());
        }
    }
}

As you can see, a user can have more than one role. For some reason, I am unable to save the roles in the database.

+5
source share
3 answers

Windows XP SP3, .NET Framework 4.5, , , , . "" "" "--", Entity Framework "" "", "" - , (Value Object) : "Entity Framework Entity", . , , " " ( ), , .

+6

, . , User ICollection<int>, - , .

Role, :

public class Role
{
    public int Id {get; set;}
    public Roles Role {get; set;}
}

enum Roles ( , Role).

+10

, -, :

public class RoleWrapper
{
    public int Id { get; set; }

    // Role is any Enum
    public Role Role { get; set; }

    public static implicit operator Role(RoleWrapper val)
    {
        return val.Role;
    }
    public static implicit operator RoleWrapper(Role val)
    {
        return new RoleWrapper() { Role = val };
    }
}

, Enum:

myUser.Roles.Add(Role.WhateverRole);
Role firstRole = myUser.Roles[0];
0

All Articles