Entity Framework - Add Navigation Property Manually
Asked Answered
K

2

121

I generated an Entity Framework Model (4.0) from my database. I did not design the database and do not have any control over the schema, but there are a few tables that do not have foreign key constraints defined, but there is an implicit relationship defined.

For example:

I have a table called People that has the following columns: GenderID RaceID

There are tables for both Gender and Race but there is no foreign key in the People table.

When I imported the model it did not add Navigation Properties for these relationships. I tried to add it manually but From Role and To Role are disabled. I'm not sure how to add the relationship myself. How do I do this?

Kosygin answered 16/12, 2010 at 22:10 Comment(0)
C
192

Yup - it's not that straightforward.

Here's what you do:

1 - Right click on the designer, Add -> Association

2 - Setup the association and cardinalities (People *..1 Gender, People *..1 Race)

3 - Go into the Model Browser -> Associations

4 - Right click on your newly created associations, click Properties

5 - Here you need to setup the endpoints for the key and cascade options. Make sure you get the endpoints correct. You can also setup a referential constraint here for your implicit navigational property.

6 - Map the navigational property to the relevant tables/fields.

7 - Validate your model, cross your fingers.

Carpophore answered 16/12, 2010 at 22:20 Comment(5)
+1 for saving what's left of my hair. I will add #6 requires changing the foreign key column in the association's properties page, Referential Constraint setting. Hit the [...] to open the referential constraint dialog and change the dummy field that the designer plugged into the child table under Dependent Property.Beauvoir
You'll also have to go to the properties of the child table and delete the dummy field that the designer added (and which doesn't map to any real fields in the child table).Beauvoir
In case of View, also make sure that you get your Primary Key definition right (right click select related fields, mark them as EntityKey in the properties). Otherwise you might get 'Multiplicity is not valid in Role in relationship Because the Dependent Role refers to the key properties...'Tutti
On #2, you might want to make sure to uncheck "Add foreign key properties to the Entity" checkbox. That creates some unnecessary headaches if you really don't need it.Samekh
Once you update again your model from database, the scaffolded entities will maintain the manually added association?Muttonchops
D
49

I came across this blog post which proposes the following solution, which worked great for me (unfortunately I could not get RPM1984's to work in my situation).

  1. Add an Association via designer background right click contextual menu
  2. Set up your Association (be sure to uncheck creation of foreign key)
  3. Right click on the association and choose Properties
  4. Click on the ... button for Referential Constraint
  5. Set up the relation between the keys within
  6. Verify (from the designer contextual menu)
  7. ???
  8. Profit!
Disabled answered 30/1, 2014 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.