ef 5 codefirst枚举集合不是在数据库中生成的 [英] ef 5 codefirst enum collection not generated in database
问题描述
我有一个在数据库中正在生成的特殊类。
虽然我的网站有点复杂,我会简化;
命名空间Store.Enities
{
public enum Role
{Manager,Clerk}
public class User
{
public int Id {get; set;}
public ICollection< Role>角色{get; set;}
}
public class StoreContext:DbContext
{
public DbSet< User>用户{get; set;}
public StoreContext()
{
Database.SetIntializer(new DropCreateDatabaseIfModelChanges< StoreContext>());
}
}
}
正如你可以看到一个用户可以有多个角色。
由于某种原因,我无法将数据库中的角色存储起来。
我还在运行Windows XP SP3所以我无法安装.NET Framework 4.5,我无法运行测试,看看有什么问题,但我可以猜到。在您的设计中,每个用户可能都有零到多个角色,现在按照约定,实体框架将在角色上生成引用用户的外键,但由于角色是一个复杂对象的枚举(Value Object)实体框架不支持这种情况,Entity Framework不支持在实体类型上包含复杂类型的集合,您需要使Role成为一个实体并给它一个ID。只是为了确保我是正确的尝试使一对一(每个用户只有一个角色),如果它的工作,那么我是对的。
I am using EF5 and .NET 4.5. I have one particular class that is being generated incorrectly in the database. Although it is somewhat more complicated in my website, I'll 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 can't manage to store the roles in the database.
I am still running Windows XP SP3 so i can't install .NET Framework 4.5 and i can't run a test to see what's wrong but i can guess. In your design each "User" may have zero-to-many "Roles", now by convention Entity Framework would generate a foreign key on "Roles" to reference "Users" but since "Role" is an enumeration which is a Complex Object (Value Object) entity framework doesn't support this situation, "Entity Framework doesn't support having a collection of a Complex Type on an Entity type", you need to make Role an entity and give it an ID. Just to be sure i am correct try making the relation one-to-one (each user has only one role) and if it worked then i am right.
这篇关于ef 5 codefirst枚举集合不是在数据库中生成的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!