您的位置:首页 > 其它

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。见下面的代码

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐