EntityType 'UserInfo' has no key defined. Define the key for this EntityType.
2013-01-10 16:50
465 查看
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。见下面的代码
后面在stackoverflow上找到了答案。EF会自动识别一个实体的主键只要主键的名称符号 'Id'或 '实体名Id'. 另外,它必须声明成属性,访问权限必须是Public的。这个错误是因为我将UserId声明成了一个字段,只要修改成属性就OK了。修改后的代码如下所示。
2013-01-10 16:50:05
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。见下面的代码
public class UserInfo { [Key] public int UserID; public string UserName; public string Password; public int UseState; public string Email; public DateTime AddTime; public int AddUser_ID; public string ImgUrl; public virtual UserType UserTypes { get; set; } }
后面在stackoverflow上找到了答案。EF会自动识别一个实体的主键只要主键的名称符号 'Id'或 '实体名Id'. 另外,它必须声明成属性,访问权限必须是Public的。这个错误是因为我将UserId声明成了一个字段,只要修改成属性就OK了。修改后的代码如下所示。
public class UserInfo { [Key] public int UserID { get; set; } public string UserName { get; set; } public string Password { get; set; } public int UseState { get; set; } public string Email { get; set; } public DateTime AddTime { get; set; } public int AddUser_ID { get; set; } public string ImgUrl{ get; set; } public virtual UserType UserTypes { get; set; } }
2013-01-10 16:50:05
相关文章推荐
- EntityType [TableName] has no key defined. Define the key for this EntityType
- SMW0报错:For selected object type excel no application has been defined Maintain the settings
- No property sql found for type xxx.xxx.entity.UserdefinedGroup
- TypeMismatchException: Provided id of the wrong type for class zhongfucheng.user.entity.User.
- TypeMismatchException: Provided id of the wrong type for class zhongfucheng.user.entity.User.
- CFURLCopyResourcePropertyForKey failed because it was passed this URL which has no scheme:
- The type 'Microsoft.Office.Interop.Excel.ApplicationClass' has no constructors defined
- Linq------错误:EntityType: EntitySet 'Products' is based on type 'Product' that has no keys defined.
- CFURLSetResourcePropertyForKey failed because it was passed this URL which has no scheme
- ASP.NET MVC another entity of the same type already has the same primary key value
- 解决错误“Logon Failure: The User Has Not Been Granted The Requested Logon Type At This Computer”
- to disable the entity lazy load, The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
- iOS User Defined Runtime Attributes to define the key path
- There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key ''.
- 《Springboot极简教程》问题解决:Springboot启动报错 Whitelabel Error Page: This application has no explicit mapping for
- setValue:forUndefinedKey this class is not key value coding-compliant for the key
- Debugger problem "The breakpoint will not currently be hit. No symbols have been loaded for this document
- Eclipse上JPA报错:Target Entity is not a entity 和the "mapped by" attribute has an invalid mapping type
- VA番茄助手提示The security key for this program currently stored on your system does not appear to be vali
- website the breakpoint will not currently be hit. no symbols have been loaded for this document