如何修复 System.Data.Edm.EdmEntityType 没有键
有谁知道如何修复此错误:
System.Data.Edm.EdmEntityType: : EntityType“BlogTags”未定义键。定义此 EntityType 的键。
将 MVC 3 与实体框架结合使用。
Does anybody know how to fix this error:
System.Data.Edm.EdmEntityType: : EntityType 'BlogTags' has no key defined. Define the key for this EntityType.
Using MVC 3 with Entity Framework.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只需将 [Key] 放在您的财产顶部(显示主钥匙)。像这样的事情,
Just put [Key] on top of your property (which is presenting primary key). Something like this,
如果实体的 Key 遵循“Id”或“EntityNameId”约定,MVC3 将自动识别它。此外,实体必须将其公开为属性,并且必须是公共的。我错误地对我的财产使用了 protected 并收到了此错误。
一个很好的例子是:
或者
如果您不能遵循此约定,或者如果您想在代码中非常明确,请使用 [Key] 属性。
MVC3 will automatically recognize an entity's Key if it follows the convention 'Id' or 'EntityNameId'. Additionally, the entity must expose this as a PROPERTY AND it must be PUBLIC. I made the mistake of using protected for my property and got this error.
A good example is:
OR
Use the [Key] attribute if you can't follow this convention OR if you want to be very explicit in your code.
该错误告诉您需要知道的所有信息:“定义此 EntityType 的键”。
在 EF 中,所有实体都必须具有某种类型的主键。
The error tells you all you need to know: "Define the key for this EntityType".
In EF all entities must have primary keys of some type.
本着分享同一问题解决方案的精神...
我遇到了同样的问题,但在使用 VS2012 编写 MVC 4 应用程序时,[Key] 解决方案并未解决该问题。
我忘记在我的模型成员中包含 getter 和 setter。仅仅公开它们是不够的,Visual Studio 会给你同样的错误消息。
我已经确定了导致此消息的 5 种情况 - 涵盖在 一篇关于它的小博客文章。
In the spirit of sharing solutions to the same problem...
I had the same problem but it wasn't fixed by the [Key] solution when coding a MVC 4 app with VS2012.
I forgot to include the getter and setters on my model members. Just having them public isn't enough and Visual Studio will give you the same error message.
I have identified 5 scenarios that result in this message - covered in a small blog post about it.
分享一些有趣的小花絮。在我的模型中,我将代码从一个 .NET 框架移植到另一个 .NET 框架,并且错过了将字符串 xml 字段转换为 XDocument 的属性。该属性应该应用了 NotMappedAttribute,但就像我说的,我忘记了,然后我开始收到这个不太具体的错误:
在模型生成过程中检测到一个或多个验证错误:
\tSystem.Data.Entity.Edm.EdmEntityType : : EntityType 'XDocument' 没有定义键。定义此 EntityType 的键。
\tSystem.Data.Entity.Edm.EdmEntitySet:EntityType:EntitySet“XDocuments”基于没有定义键的类型“XDocument”。
我追了大约一个小时,因为错误发生在我的 DbContext 类公开的其他模型之一上。出于沮丧,我搜索了程序集中的每个 XDocument 属性和 BAMB!发现一个没有 NotMapped 属性。
只是想把它放在那里以防止其他人拔掉他们的头发。
例子:
Intersting little tid-bit to share. On my model, I was porting my code from one .NET framework to another and missed a property that converted a string xml field to an XDocument. The property should have had a NotMappedAttribute applied, but like I said, I forgot and then I started getting this not-very-specific error:
One or more validation errors were detected during model generation:
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'XDocument' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'XDocuments' is based on type 'XDocument' that has no keys defined.
I chassed my tail for about an hour because the error was happening on one of the other models exposed by my DbContext class. Out of frustration, I hunted through every XDocument property in my assembly and BAMB! Found one that did not have the NotMapped attribute.
Just wanted to put that out there to prevent someone else from pulling their hair out.
Example:
与@Gilesey 的情况相同。就我而言,我必须将关键属性标记为 public,
public int Id { get;放; }
Same scenario as @Gilesey. In my case I had to mark the key attribute public,
public int Id { get; set; }