实体框架代码首先 - 定义此EntityType的键 [英] Entity Framework Code-First - Define the key for this EntityType
问题描述
我有三个表,结构如下
public partial class App_user
{
public int id {get;组; }
public string name {get;组; }
public string email_address {get;组; }
public string password {get;组; }
public int user_type {get;组; }
public List< Role>角色{get;组; }
}
public partial class Role
{
public int id {get;组; }
public string name {get;组; }
}
public partial class User_role
{
public int user_id {get;组; }
public int role_id {get;组; }
public virtual Role Role {get;组; }
public virtual App_user App_user {get;组; }
}
在第三个表中没有主键。所以运行时会出错。以下是错误消息 -
System.Data.Edm.EdmEntityType::
EntityType'User_role'没有键
定义。定义此
EntityType的键。
System.Data.Edm.EdmEntitySet:
EntityType:EntitySet User_roles
基于没有定义
键的User_role类型。
< blockquote>
为什么它发生了?是否有解决方案?
解决方案如果您试图建立用户和角色之间的多对多关系。在这种情况下,您的模型是完全错误的。
使用此代替:
public partial class App_user
{
public int id {get;组; }
public string name {get;组; }
public string email_address {get;组; }
public string password {get;组; }
public int user_type {get;组; }
public virtual ICollection< Role>角色{get;组; }
}
public partial class Role
{
public int id {get;组; }
public string name {get;组; }
public virtual ICollection< User>用户{get;组; }
}
这将自动创建多对多,您不需要麻烦连接台。如果你需要暴露连接表,你必须使用这个:
public partial class App_user
{
public int id {get;组; }
public string name {get;组; }
public string email_address {get;组; }
public string password {get;组; }
public int user_type {get;组; }
public virtual ICollection< User_Role> UserRoles {get;组; }
}
public partial class Role
{
public int id {get;组; }
public string name {get;组; }
public virtual ICollection< User_Role> UserRoles {get;组;
public partial class User_role
{
[Key,ForeignKey(App_user),Column(Order = 0)]
public int user_id {get;组; }
[Key,ForeignKey(Role),Column(Order = 1)]
public int role_id {get;组; }
public virtual Role Role {get;组; }
public virtual App_user App_user {get;组; }
}
如果您不需要其他属性,则露出连接表是无意义的。
对于您的错误 - Entity框架中的每个实体都必须定义主键。
Hi I'm planning to test EF Code First in one of my project. This is what I want actually. I have three tables and the structure is as follows
public partial class App_user { public int id { get; set; } public string name { get; set; } public string email_address { get; set; } public string password { get; set; } public int user_type { get; set; } public List<Role> Roles { get; set; } } public partial class Role { public int id { get; set; } public string name { get; set; } } public partial class User_role { public int user_id { get; set; } public int role_id { get; set; } public virtual Role Role { get; set; } public virtual App_user App_user { get; set; } }
In the third table there is no primary key. So it gives an error while running. Here is the error message -
System.Data.Edm.EdmEntityType: : EntityType 'User_role' has no key defined. Define the key for this EntityType. System.Data.Edm.EdmEntitySet: EntityType: The EntitySet User_roles is based on type User_role that has no keys defined.
Why its is happening? Is there a solution for this?
解决方案If think you are trying to model many-to-many relation between user and role. In such case your model is completely wrong.
Use this instead:
public partial class App_user { public int id { get; set; } public string name { get; set; } public string email_address { get; set; } public string password { get; set; } public int user_type { get; set; } public virtual ICollection<Role> Roles { get; set; } } public partial class Role { public int id { get; set; } public string name { get; set; } public virtual ICollection<User> Users { get; set; } }
This will create many-to-many automatically and you will not need to bother with junction table. If you need to expose junction table you must use this:
public partial class App_user { public int id { get; set; } public string name { get; set; } public string email_address { get; set; } public string password { get; set; } public int user_type { get; set; } public virtual ICollection<User_Role> UserRoles { get; set; } } public partial class Role { public int id { get; set; } public string name { get; set; } public virtual ICollection<User_Role> UserRoles { get; set; } } public partial class User_role { [Key, ForeignKey("App_user"), Column(Order = 0)] public int user_id { get; set; } [Key, ForeignKey("Role"), Column(Order = 1)] public int role_id { get; set; } public virtual Role Role { get; set; } public virtual App_user App_user { get; set; } }
Exposing junction table is meaningless if you don't need additional properties in it.
To your error - each entity in Entity framework must have primary key defined.
这篇关于实体框架代码首先 - 定义此EntityType的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!