0

In a database I have two tables User and Organization as well as a bridging table Users_Orgs.

The Users_Orgs table has a composite key comprising of two columns userId and organizationId. When I use the designer to create an association it creates two principals for the referential constraint - Users and Users_Orgs. The problem is with the Users_Orgs constraint - it has a key comprising of both organizationId and userId. Since organizationId is not in the User table, it cannot be mapped and therefore won't compile.

Users_Orgs table Users_Orgs Constraints

When clicking the 'Delete' button the dialog just closes and organizationId is just mapped to the primary key for the users table (which is not userId)

How can you do many to many mappings with composite keys on an existing database?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
SamWM
  • 5,196
  • 12
  • 56
  • 85

1 Answers1

1

Why do you have bridging table in your model? It is not needed if you don't have additional properties in this table. Otherwise you should just model relation between Users and Organizations and correctly configure many-to-many multiplicity. EF will automatically create bridging table and mapping for this relation.

To solve your issue: Bridging table is dependent, not principal. Switch roles in the constraint and map User.UserId PK to Users_Orgs.UserId FK.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • The bridging table is not my creation, it is from an existing database. I can't change the database as it is part of a CMS (Liferay) and could change with hotfixes and updates – SamWM Feb 06 '12 at 14:54