博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
EntityType 'UserInfo' has no key defined. Define the key for this EntityType.
阅读量:6922 次
发布时间:2019-06-27

本文共 1516 字,大约阅读时间需要 5 分钟。

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

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

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

你可能感兴趣的文章
MySql入门教程mysql基础知识学习实例讲解
查看>>
const的理解
查看>>
cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
查看>>
设计模式系列 - Composite模式
查看>>
linux配置修改ip地址
查看>>
Shell脚本之Mysql授权
查看>>
开始写博客了
查看>>
SCCM 2007 实现PXE要点
查看>>
[转载]Windows和Linux双系统下完美卸载linux
查看>>
oracle:检查操作系统版本: 必须是 XXX 未通过
查看>>
python中的行结构和缩进
查看>>
Linux 基于openssl的https服务配置
查看>>
磨刀不误砍柴工 建站前选好虚拟主机是关键
查看>>
sed命令小总结(一)
查看>>
遇到的vSphere Client无法连接vSphere server的问题
查看>>
我的友情链接
查看>>
Ubuntu 配置JDK
查看>>
八款开源 Android 游戏引擎 (巨好的资源)
查看>>
lnmp源码安装
查看>>
数据库事务基础知识
查看>>