How do I correctly set an association between two objects in the Entity Framework 4 Entitydesigner?
Asked Answered
C

8

54

For a new project I'm trying to create my business classes first and create the real database tables later. Therefore I'm using the Entity Framework 4 Designer. A created a new "ADO.Net Entity Data model" file, with the extension .edmx.

I created two Entities:
alt text

I want to add a 1 to nc relation between Product -> Group. If I'd created the MSSQL database first, I would have added a column IDGroup to the Table Product and referenced Product.IDGroup to Group.IDGroup. As far as I can see, I can't add such association in the designer if I add a new Property called IDGroup to the Product Entity

This is how I add the mapping: alt text

Which results in:
alt text

Now the part what this question is about: If I add two tables from an existing MSSQL database to the edmx file, I'll get the compile error:

Error 3027: No mapping specified for the following EntitySet/AssociationSet - GroupSet, ProductSet

What does that error mean and what must I do to fix this? If I delete those two tables, I'll receive a warning instead:

Error 2062: No mapping specified for instances of the EntitySet and AssociationSet in the EntityContainer myContainer.

Something tells me, I'm doing this all wrong and this is just basic stuff. How can I do it right?

Curzon answered 13/9, 2010 at 13:4 Comment(1)
+1 Thanks for asking this one, I routinely forget to do this.Pomfrey
O
85

I just ran into this myself & when I Googled it (I hit your question).

I was able to double click on the line (association line) in the designer. Fill in the properties there and I got it working.

I also had to close VS and re-open it to get some of the errors reported to go away...

Can't say this is the correct answer - just fumbling around and seeming to get results.

Osset answered 13/9, 2010 at 18:55 Comment(5)
Thank you! I was going through Properties and Mapping, and creating the link in different ways, but no matter what I did I couldn't get the error to go away. I never thought to double-click the link.Scupper
I had double-clicked the link and set the referential constraint--but the the error remained. Restarting the VS 2010 and setting the referential constraint again did the trick.Itinerant
Nice answer. I was surprised to see that I needed to close and reopen VS to make it work, even after doing everything else correctly. Entity Framework is awesome, except when it isn't!Kidney
@Jesslyn, you should post your own answer ;) You have saved my day. Thanks. (VS2013)Isolated
A more detailed explanation of why this works is here: #4466355Nadbus
K
9

Something that might be easy to miss is to Generate DataBase from model. From what I understand this has to be run after every change made to the Database .edmx.

This simple mistake happened to me and lead to frustration for a few minutes =)

Klarrisa answered 29/3, 2012 at 13:55 Comment(0)
C
7

I got this error when I generated the model from the database using the ADO.Net DBContext Generator under VS2010 SP1 with EF4.1 installed. The database I was using did NOT have any referential integrity between the primary and foreign key fields.

The solution was to double click the association line which brings up the Referential Contstrainst dialog box. I then set the Principal field to be the table with the primary key, the Dependent as the table with the foreign key, set the foreign key names to match in the drop down and clicked OK.

The code then compiled and I was able to work across the entities using var foreign = DBContext.Primary.First().Foreign etc

Steve

Cannibalism answered 9/5, 2011 at 14:50 Comment(0)
P
3

I had the same error, and one bit of configuration that the "Add Association" dialog didn't help me set was the "Referential Constraint" property. Once I configured that I rebuilt the solution and everything was cool.

Pomfrey answered 5/5, 2011 at 1:14 Comment(0)
J
0

I've had this error when I've deleted an association from the model diagram but the association remains in the underlying XML in the .edmx. They only way I've found to fix that is to hand-edit the edmx to remove references to the offending association.

Jonajonah answered 4/12, 2013 at 20:26 Comment(0)
D
0

All you just have to do is delete all your tables from edmx file. Now right click in edmx file and choose update form database option. Update Ef, issue will be resolved.

Daybook answered 4/4, 2016 at 13:53 Comment(0)
M
0

I am using database first approach and encountered the same issue.

what was causing the problem is on of my coworker deleted one of the table from the database and forgot to update the model.

My solution is find the deleted entity from the diagram. (model browser could be useful.) Then delete it from the diagram, since EF doesn't remove entities if you deleted the table from the database.

Build, and check Error List window (Ctrl+E,W).

Done.

Megagamete answered 18/11, 2016 at 20:58 Comment(0)
A
0

I have also found that rebuilding your project after changes to your Entity Framework context and models can sometimes resolve these errors, especially if the Entity Framework contexts are in a different project.

Astrosphere answered 25/1, 2017 at 22:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.