.net 如何修复 System.Data.Edm.EdmEntityType 没有键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5277788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:22:11  来源:igfitidea点击:

How to fix System.Data.Edm.EdmEntityType has no key

.netasp.net-mvclinqentity-framework

提问by RPS

Does anybody know how to fix this error:

有谁知道如何解决这个错误:

System.Data.Edm.EdmEntityType: : EntityType 'BlogTags' has no key defined. Define the key for this EntityType.

System.Data.Edm.EdmEntityType:: EntityType 'BlogTags' 没有定义键。定义此 EntityType 的键。

Using MVC 3 with Entity Framework.

将 MVC 3 与实体框架一起使用。

采纳答案by CodingGorilla

The error tells you all you need to know: "Define the key for this EntityType".

该错误告诉您所有您需要知道的信息:“定义此 EntityType 的键”。

In EF all entities must have primary keys of some type.

在 EF 中,所有实体都必须具有某种类型的主键。

回答by Ishraq Ahmad

Just put [Key] on top of your property (which is presenting primary key). Something like this,

只需将 [Key] 放在您的财产(呈现主键)之上。像这样的东西,

[Key]
public int BlogTypeId { get; set; }

回答by kingdango

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.

如果实体的 Key 遵循约定“Id”或“EntityNameId”,MVC3 将自动识别该实体的 Key。此外,实体必须将其作为属性公开,并且必须是公开的。我错误地为我的财产使用了 protected 并得到了这个错误。

A good example is:

一个很好的例子是:

public int Id { get; set; }

OR

或者

public int EntityNameId { get; set; }

Use the [Key] attribute if you can't follow this convention OR if you want to be very explicit in your code.

如果您不能遵循此约定或希望在代码中非常明确,请使用 [Key] 属性。

回答by GilesDMiddleton

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.

我遇到了同样的问题,但在使用 VS2012 编写 MVC 4 应用程序时,[Key] 解决方案没有解决这个问题。

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.

我忘了在我的模型成员中包含 getter 和 setter。仅仅让它们公开是不够的,Visual Studio 会给你同样的错误信息。

I have identified 5 scenarios that result in this message - covered in a small blog post about it.

我已经确定了导致此消息的 5 个场景 - 在一篇关于它的小博客文章中进行了介绍

回答by RashadRivera

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:

有趣的小花絮分享。在我的模型中,我将代码从一个 .NET 框架移植到另一个框架,但遗漏了一个将字符串 xml 字段转换为 XDocument 的属性。该属性应该应用了 NotMappedAttribute,但就像我说的,我忘记了,然后我开始收到这个不太具体的错误:

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.

\tSystem.Data.Entity.Edm.EdmEntityType:: EntityType 'XDocument' 没有定义键。定义此 EntityType 的键。\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'XDocuments' 基于未定义键的类型 'XDocument'。

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.

我追了大约一个小时,因为错误发生在我的 DbContext 类公开的其他模型之一上。出于沮丧,我搜索了我的程序集中的每个 XDocument 属性和 BAMB!发现一个没有 NotMapped 属性的。

Just wanted to put that out there to prevent someone else from pulling their hair out.

只是想把它放在那里,以防止别人把头发拔出来。

Example:

例子:

// This NotMappedAttribute was missing and is required to prevent EF
// from treating the XDocument class as an entity that requires
// a KeyAttribute.
[NotMapped] //<== missing until now
public XDocument DataXml {
    get { return XDocument.Parse(this.m_Xml); }
    set {
        this.m_Data = value.ToString();
    }
}

回答by kannankeril

Same scenario as @Gilesey. In my case I had to mark the key attribute public,

与@Gilesey 相同的场景。就我而言,我必须将关键属性标记为公开,

public int Id { get; set; }

公共 int Id { 获取;放; }