I am using the VS 2010 Entity Framework designer to modify some of the entities that were auto-generated from a database.
The database structure is defined by a product I am building a UI for so I can’t modify it.
The database has tables that have a many-to-many relationship based purely around common values in non-key columns on both tables.
- Table 1 Users has a single id column userId and another column called userProfile.
- Table 2 SecurityProfile has a single id column securityProfileId and also has column userProfile.
In other words a User Profile is just a name for a mapping of users to SecurityProfiles.
The wizard created entities for both the Users and SecurityProfile tables. I created a new entity called UserProfile with a single key field: userProfile.
I created a one-to-many association from UserProfile to User and from UserProfile to SecurityProfile. This all works fine except that the UserProfile entity has no table mapping so it won’t compile.
How do I create this association in Entity Framework?
UPDATE:
If it’s not possible using EF (as is suggested by the first answer), is it possible to manually create a non-bound entity that I can use to link these two entities so that I expose an object model that is at least a little easier to navigate?
i.e. I would make the User and SecurityProfile entities abstract and manually define an extension of these with the appropriate properties pointing to my manually created UserProfile entity
You will not. EF is not able to work with many to many association which doesn’t exist in database. EF demands same referential integrity as database – in this case it demands junction table to exist and be mapped to association used in diagram.