views:

59

answers:

1

I was working in L2E with 3 simple tables, whose purposes a fairly straightforward: Users, Users_Roles, Roles.

Users had a 1:many relationship with Users_Roles, referenced on the column UserID (a uniqueidentifier). At the time, Roles had no relation to any tables. I brought all 3 tables into the designer, which reflected the mapping of Users to Users_Roles, while Roles sat all by itself.

Realizing Roles should be mapped to Users_Roles in a many:many relationship, I jumped into management studio. I slapped a relationship from Roles to Users_Roles and saved it. I then jumped back into VS and did the logical the next logical step in my mind - tried to update the Entity model by right-clicking, "Update Model from database". It showed all three tables in its update list. After updating, the visual relationship didn't change. I tried to recompile to see if any changes were made, but received two errors differing only in line numbers:

Error 3 Error 3034: Problem in Mapping Fragments starting at lines 177, 192: 
                    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.

C:*\Model1.edmx 178 15  MVCTestApp

Tried to debug the error to no avail. Eventually got frustrated, deleted the model and rebuilt it. The relationships were recognized, the designer was update properly, life was good.

This isn't the first time I've wanted to take the designer outside and bury it, either: a week or so earlier I had the nerve to delete a table, and learned very quickly that deleting a table in the designer only deletes it from the designer; its mappings stay.

So, a few questions:

1) is this behavior limited to VS 2008? I'll have 2010 shortly, and am hoping that the designer functions as expected,

2) are there other tools that can replaced the built-in designer that actually work,

3) is there a way to make update from model actually update - perhaps some trick I'm not aware of besides deleting the entire model, thus losing all my other relationships I've set up manually?

A: 

Broken? It's more correct to say that it only recognizes certain DB schema patterns.

The EF supports other patterns, but you need to write the EDMX yourself. If you want the GUI Designer to get everything right for you, you need to follow the patterns it expedcts.

If Users_Roles has only two columns, the UserId and the RoleId and if those are both FKs to their respective tables, and if the two columns together are the PK of the table, then mapping the table will come out right with no work on your part.

Otherwise, you have to study the EDMX format and get the mapping right yourself. As you've discovered, that's trickier.

Craig Stuntz
The problem was not the mapping. The problem is that the designer itself does not properly update the mappings when refreshed. It only correctly recognizes the mappings if they exist before being added to the .edmx; if I add the tables, then change their schema in MSSQL, the new mappings (despite being 100% correct) are not recognized and I get the error in question.
lynx_guy
I understand all of that. The issue is that the GUI designer regenerates SSDL, but not CSDL or MSL. It presumes you may want to keep any customizations you've made. You need to clear out the mapping schema in the EDMX for the associations if you substantively change the schema.
Craig Stuntz
Thanks for the clarification - I'm now up to my eyeballs in learning CSDL and MSL, hopefully that will make life easier in the future.
lynx_guy