EntityType 'UserInfo' has no key defined. Define the key for this EntityType.

简介:

One or more validation errors were detected during model generation:

 System.Data.Edm.EdmEntityType: : EntityType 'UserInfo' has no key defined. Define the key for this EntityType. System.Data.Edm.EdmEntitySet: EntityType: EntitySet �UserInfo� is based on type �UserInfo� that has no keys defined.

遇见这个问题,我觉得很奇特,因为事实上我已经为'UserInfo'这个类定义了[KEY]的类注释。

然后又提示我找不到Key。见下面的代码

复制代码
 1     public class UserInfo
 2     {
 3         [Key]
 4         public int UserID;
 5         public string UserName;
 6         public string Password;
 7         public int UseState;
 8         public string Email;
 9         public DateTime AddTime;
10         public int AddUser_ID;
11         public string ImgUrl;
12         public virtual UserType UserTypes { get; set; }
13     }
复制代码

后面在stackoverflow上找到了答案EF会自动识别一个实体的主键只要主键的名称符号 'Id'或 '实体名Id'. 另外,它必须声明成属性,访问权限必须是Public的。这个错误是因为我将UserId声明成了一个字段,只要修改成属性就OK了。修改后的代码如下所示。

 

复制代码
 1     public class UserInfo
 2     {
 3         [Key]
 4         public int UserID { get; set; }
 5         public string UserName { get; set; }
 6         public string Password { get; set; }
 7         public int UseState { get; set; }
 8         public string Email { get; set; }
 9         public DateTime AddTime { get; set; }
10         public int AddUser_ID { get; set; }
11         public string ImgUrl{ get; set; }
12         public virtual UserType UserTypes { get; set; }
13     }
复制代码

 

 

 

 

2013-01-10  16:50:05


作者:kissazi2 
出处:http://www.cnblogs.com/kissazi2/ 
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

转载:http://www.cnblogs.com/kissazi2/archive/2013/01/10/2855082.html

目录
相关文章
|
26天前
|
PHP
Trying to access array offset on value of type null
你就可以避免在null值上尝试访问数组偏移量的错误。 总的来说,当你遇到这个错误时,你应该回顾你的代码,确保在尝试访问数组偏移量之前,相关的变量已经被正确地初始化为一个数组,并且不是null。
|
7月前
|
关系型数据库 MySQL 数据库
解决出现的SQLIntegrityConstraintViolationExceptionw:Duplicate entry ‘10‘ for for key ‘user.PRIMARY‘问题
解决出现的SQLIntegrityConstraintViolationExceptionw:Duplicate entry ‘10‘ for for key ‘user.PRIMARY‘问题
complex_key_cache
complex_key_cache
48 0
When allowCredentials is true, allowedOrigins cannot contain the special value ___ since that cannot be set on the _Access-Contr
When allowCredentials is true, allowedOrigins cannot contain the special value ___ since that cannot be set on the _Access-Contr
354 1
|
存储