为什么我得到这个错误: 没有为下面的 EntitySet/AssociationSet-Entity1指定映射?

我正在使用实体框架4和模型优先方法。

我启动了这个项目,设计了实体并生成了数据库,一切都运行良好。

然后我需要返回并向模型中添加另一个实体。然而,当我将一个实体拖动到 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.

我如何处理这个错误?

144075 次浏览

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

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.

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:

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?

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.

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.

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

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.

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.

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.

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

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]

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".

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.

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

  1. Go to Solution Explorer, click the Search button
  2. Leave checked both Search within file content and Search External Files
  3. Type the entities name which your mapping isn't recognizing.
  4. Delete all files RELATED to the problem. Those will probably be named after the same missing entity. DO NOT delete any file with your Context Class name on the file, specially if their extensions are .cs or .tt. In the Context .cs file.
  5. remove all lines of codes referencing the missing entity. They will look like this:

    public virtual DbSet< Entity1> Entity1 { get; set; }
    

This error is common to tables deleted from the database.

When one drops a table in the database, or one just changes the web.config.connectionStrings for the EF Mapped database, to point to a new one and not the one used to generate the original mappings is the problem.

It is this new db these entities with the 3027 error aren't present.

Had this error when I had deleted a table from the database. Solved it by right clicking on EDMX diagram, going to Properties, selecting the table from the list in the Properties window, and deleting it (using delete key) from the diagram.

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

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.

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

I had the error when I was trying to make a custom result for a stored procedure and assumed it had to be an entity.

The solution was that I just made a complex type in the Model browser and assigned that as a result to the "Edit function imports".

I will add it here since it looks like this question is where google takes you when you get this error.

I had set everything correctly (cardinalities and dependent properties) but could not figure out why I keep receiving error. Finally figured out that, EF generated a column in dependent table on its own (table_tablecolumn) and it does not have any relation to the table, so no mapping was specified. I had to delete the column in EDMX file and rebuild the solution which fixed the issue. I am using DB approach.

For those who are using Database First approach all you have to do after inserting a new entity is to Generate Database From Model again by right click on your .edmx file and select Generate Database From Model...

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.

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.

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

I think I got this from not explicitly deleting some tables from the edmx before renaming and re-adding them. Instead, I just renamed the tables and then did an Update Model from Database, thinking it would see them gone, and delete them from model. I then did another Update Model from Database and added the renamed tables.

The site was working with the new tables, but I had the error. Eventually, I noticed the original tables were still in the model. I deleted them from the model (click them in edmx screen, delete key), and then the error went away.

If you are not using model-first; Double click 'edmx' file ->select all and delete all entity models -> save -> right click 'update model from database ->select required tables ->finish and save.