In Visual Studio using Entity Framework 5.0, I receive the following error when working with link tables (many-to-many mapping tables):
Error 3034: Problem in mapping fragments starting at lines:Two entities with possibly different keys are mapped to the same row. Ensure these two mapping fragments map both ends of the AssociationSet to the corresponding columns.
First, it really helps to clearly understand how Entity Framework handles many to many mapping tables. If a linking table has any fields other than the two linking ID fields, it will cause Entity Framework some trouble, but doesn’t cause this error. It is also important to specify the foreign keys so Entity Framework knows what the fields are for. For great detail, see http://weblogs.asp.net/zeeshanhirani/archive/2008/08/21/many-to-many-mappings-in-entity-framework.aspx
After a lot of searching and manipulation of Primary Keys (PK) and Foreign Keys (FK) I found that no logical action fixed the problem. Theoretically you could edit the EDMX (XML) file and fix something, but it can be thousands of lines long and very cryptic. You aren’t supposed to have to edit the EDMX file – certainly not for an error like this. Again, theoretically you should be able to “Update Model from Database”, but it doesn’t resolve this problem.
Entity Framework Resolution
The resolution that seems to work for many and also worked for me was to delete the EDMX file and rebuild it. The only warning is that you must specify the same names in the Wizard or your class names won’t match. It isn’t worth the trouble to try to rename the classes. If you find that you have changed the class names, you can also delete and rebuild your .tt/.cs files and regenerate them too. At this point, you should have very few class references that are incorrect.
For information about how Acumen can help with your Software needs, contact us today!