为什么我得到这个错误:没有为以下EntitySet / AssociationSet - Entity1指定映射? [英] Why am I getting this error: No mapping specified for the following EntitySet/AssociationSet - Entity1?
问题描述
我正在使用具有Model First方法的Entity Framework 4。
I'm using Entity Framework 4 with the Model First approach.
我启动了项目,设计了实体并生成了数据库。一切正常。
I started the project, designed the entities and generated the database. Everything worked fine.
然后我需要返回并在我的模型中添加另一个实体。但是,当我拖动一个实体到EDMX我得到这个错误:
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:
好的!我只需要将Entity1映射到一个表..但嘿!我正在使用Model First方法,我希望在生成DDL时为我创建表。
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.
我如何解决这个错误?
推荐答案
这是因为EF4与模特儿一起工作。
This is because of the way EF4 works with model-first.
您首先创建一个模型第一模型,它处于SSDL不存在的状态。您可以拖动实体,关联它们等等,但是如果您查看EDMX文件上的SSDL,您将看到没有一个实体在SSDL中具有关联的存储表。
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.
当您单击从模型生成数据库
上下文菜单项时,会发生更改。令人困惑的部分是,此操作不仅仅是生成DDL脚本。实际上,它将EDMX文件更改为包括SSDL信息。从这一点开始,EDMX文件将进入设计器/ CSDL中的每个实体必须映射到SSDL中的实体的状态。如果没有映射,它将触发编译时错误:
That changes when you click the Generate Database From Model
context 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:
没有为以下EntitySet / AssociationSet指定的映射 - (EntityName)
No mapping specified for the following EntitySet/AssociationSet - (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?
为了防止这个错误,插入一个新实体之后你要做的所有事情是生成数据库再次从模型
这将更新SSDL并修复映射。
To prevent this error, All you have to do after inserting a new entity is to Generate Database From Model
again. That will update the SSDL and fix the mappings.
编辑
如果你没有使用模型优先,并且从数据库更新,您还将在DB Server中删除表时出现此错误。这是因为实体框架不会自动删除您的实体。手动删除实体,错误将消失。
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.
这篇关于为什么我得到这个错误:没有为以下EntitySet / AssociationSet - Entity1指定映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!