.net 为什么我会收到此错误:没有为以下 EntitySet/AssociationSet - Entity1 指定映射?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5667355/
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:29:05  来源:igfitidea点击:

Why am I getting this error: No mapping specified for the following EntitySet/AssociationSet - Entity1?

.netentity-frameworkentity-framework-4

提问by André Pena

I'm using Entity Framework 4 with the Model First approach.

我将 Entity Framework 4 与 Model First 方法一起使用。

I started the project, designed the entities and generated the database. Everything worked fine.

我开始了这个项目,设计了实体并生成了数据库。一切正常。

Then I needed to go back and add another entity to my model. However, as I drag an entity to the EDMX I get this error:

然后我需要返回并将另一个实体添加到我的模型中。但是,当我将实体拖到 EDMX 时,出现此错误:

enter image description here

在此处输入图片说明

Alright! I just need to map Entity1 to a table.. But hey! I'm using Model First approach, I expect it to create the table for me when I generate the DDL.

好吧!我只需要将 Entity1 映射到一个表..但是嘿!我正在使用模型优先方法,我希望它在生成 DDL 时为我创建表。

How do I work around this error?

我该如何解决这个错误?

回答by André Pena

This is because of the way EF4 works with model-first.

这是因为 EF4 使用模型优先的方式。

When you first create a model-first model, it's in a state that the SSDL does not exist. You can drag entities, associate them and so forth and yet, if you take a look at the SSDL on the EDMX file, you will see that none of the entities have an associated storage table in the SSDL.

当您第一次创建模型优先模型时,它处于 SSDL 不存在的状态。您可以拖动实体、关联它们等等,但是,如果您查看 EDMX 文件上的 SSDL,您将看到没有任何实体在 SSDL 中具有关联的存储表。

That changes when you click the Generate Database From Modelcontext menu item. The confusing part is that this action does more than simply generating a DDL script. In fact, it changes the EDMX file to include SSDL information. From this point on, the EDMX file will enter a state in which every entity in the designer/CSDL must map to an entity in the SSDL. If one does not map, it will trigger a compile time error:

当您单击Generate Database From Model上下文菜单项时,情况会发生变化。令人困惑的部分是此操作不仅仅是生成 DDL 脚本。实际上,它更改了 EDMX 文件以包含 SSDL 信息。从此时起,EDMX 文件将进入一种状态,其中设计器/CSDL 中的每个实体都必须映射到 SSDL 中的一个实体。如果没有映射,则会触发编译时错误:

No mapping specified for the following EntitySet/AssociationSet - (EntityName)

没有为以下实体集/关联集指定映射 - (EntityName)

Another interesting fact is that it's not the kind of error that will prevent compilation. It will, indeed, generate the output class library. Shouldn't it be a warning or something?

另一个有趣的事实是,它不是那种会阻止编译的错误。它确实会生成输出类库。不应该是警告什么的吗?

To prevent this error, All you have to do after inserting a new entity is to Generate Database From Modelagain. That will update the SSDL and fix the mappings.

为防止出现此错误,在插入新实体后您要做的就是Generate Database From Model再次执行。这将更新 SSDL 并修复映射。

EDIT

编辑

If you are not using model-first and you "update from database", you will also have this error in the case you deleted a table in DB Server. This is because Entity Framework will not automatically delete the entity for you. Delete the entity manually and the error will go away.

如果您不使用模型优先并且“从数据库更新”,则在删除 DB Server 中的表的情况下也会出现此错误。这是因为实体框架不会自动为您删除实体。手动删除实体,错误就会消失。

回答by Mal

I found I was getting the same error because I had forgot to create referential constraint after creating an association between two entities.

我发现我遇到了同样的错误,因为我在创建两个实体之间的关联后忘记创建引用约束。

回答by Azeem ahmad

Error 3027: No mapping specified for the following EntitySet/AssociationSet ..." - Entity Framework headaches

错误 3027:没有为以下 EntitySet/AssociationSet 指定映射......” - 实体框架问题

If you are developing model with Entities Framework then you may run into this annoying error at times:

如果您正在使用实体框架开发模型,那么您有时可能会遇到这个烦人的错误:

Error 3027: No mapping specified for the following EntitySet/AssociationSet [Entity or Association Name]

错误 3027:没有为以下 EntitySet/AssociationSet [Entity or Association Name] 指定映射

This may make no sense when everything looks fine on the EDM, but that's because this error has nothing to do with the EDM usually. What it should say is "regenerate your database files".

当 EDM 上一切正常时,这可能没有意义,但这是因为此错误通常与 EDM 无关。它应该说的是“重新生成您的数据库文件”。

You see, Entities checks against the SSDL and MSL during build, so if you just changed your EDM but doesn't use Generate Database Model...then it complains that there's stuff missing in your sql scripts.

你看,实体在构建过程中检查 SSDL 和 MSL,所以如果你只是改变了你的 EDM 但不使用生成数据库模型......那么它会抱怨你的 sql 脚本中缺少一些东西。

so, in short, the solution is: "Don't forget to Generate Database Modelevery time after you update your EDM if you are doing model first development. I hope your problem is solved".

因此,简而言之,解决方案是:“如果您是先进行模型开发,请不要忘记 每次更新 EDM 后生成数据库模型。我希望您的问题得到解决”。

回答by Graham Laight

In my case, another developer had removed some of the tables from the underlying database. When I realised this, and removed these tables from the entity, the problem was solved. Wasn't as obvious as it sounds.

就我而言,另一位开发人员已从底层数据库中删除了一些表。当我意识到这一点并从实体中删除这些表时,问题就解决了。并不像听起来那么明显。

回答by batpox

I ran into the same error, but I was not using model-first. It turned out that somehow my EDMX file contained a reference to a table even though it did not show up in the designer. Interestingly, when I did a text search for the table name in Visual Studio (2013) the table was not found.

我遇到了同样的错误,但我没有使用模型优先。结果是我的 EDMX 文件以某种方式包含对表格的引用,即使它没有出现在设计器中。有趣的是,当我在 Visual Studio (2013) 中对表名进行文本搜索时,没有找到该表。

To solve the issue, I used an external editor (Notepad++) to find the reference to the offending table in the EDMX file, and then (carefully) removed all references to the table. I am sorry to say that I do not know how the EDMX file got into this state in the first place.

为了解决这个问题,我使用了一个外部编辑器 (Notepad++) 在 EDMX 文件中找到了对违规表的引用,然后(小心地)删除了对表的所有引用。很抱歉,我不知道 EDMX 文件最初是如何进入这种状态的。

回答by ΩmegaMan

I had a table change and it created another entity with a number 1 at the end (such as MyEntity1and a MyEntity) as confirmed by the edmx model browser. Something about the two entities together confused the processing.

我有一个表更改,它创建了另一个带有数字 1 的实体(例如MyEntity1和 a MyEntity),正如 edmx 模型浏览器所确认的那样。关于这两个实体的一些事情一起混淆了处理。

Removal of the table and re-adding it fixed it.

删除表并重新添加它修复了它。



Note that if TFS is hooked up, then do a check-in of the edmx in after the delete. Then and only then get the latest and re-add it in a definite two step process. Otherwise TFS gets confused with the delete and re-add of same named entity(ies) which seems to cause problems.

请注意,如果 TFS 已连接,则在删除后对 edmx 进行检入。然后,然后才获取最新的并在明确的两步过程中重新添加它。否则 TFS 会与相同命名实体的删除和重新添加混淆,这似乎会导致问题。

回答by Atul K.

A quicker way for me was to delete the tables and re-add them. It Auto-mapped them. :)

对我来说更快的方法是删除表格并重新添加它们。它自动映射它们。:)

回答by Masoud Darvishian

For those who are using Database Firstapproach all you have to do after inserting a new entity is to Generate Database From Modelagain by right click on your .edmxfile and select Generate Database From Model...

对于那些使用Database First方法的人,在插入新实体后,您要做的就是Generate Database From Model再次右键单击您的.edmx文件并选择Generate Database From Model...

回答by Willy David Jr

Update Model from Database doesn't works for me.

从数据库更新模型对我不起作用。

I had to remove the conflicted entity, then execute Update Model from Database, lastly rebuild the solution. After that, everything works fine.

我必须删除冲突的实体,然后从数据库执行更新模型,最后重建解决方案。之后,一切正常。

回答by AdorableVB

Sharing this for other people. In my case, we were working on a shared MVC solution, and using a common module for tables that we use for dropdowns. I got the error when I updated the Entity model by adding a new table. It turns out that when I updated the the EDMX, it probably updated my rights access on the database, which resulted to not having access to that certain table giving me no mapping specified.

分享给其他人。就我而言,我们正在开发一个共享的 MVC 解决方案,并为我们用于下拉列表的表使用一个通用模块。通过添加新表更新实体模型时出现错误。事实证明,当我更新 EDMX 时,它可能更新了我对数据库的权限访问,这导致无法访问特定表给我no mapping specified.

Just re-adding and giving access to my user solved the problem.

只需重新添加并授予我的用户访问权限即可解决问题。