69

Click here to see how to do it while using a code first approach. Below the original database first approach question and answer.

I am using Entity Designer to create a database first model. Now I want to create a foreign key relationship between ProductId and ProductId (see below). I looked at the "add new association" but I cannot connect the two items. Can someone more experienced tell me how to accomplish this?

foreign key

Community
  • 1
  • 1
user2609980
  • 10,264
  • 15
  • 74
  • 143

3 Answers3

47

According to Relationships/Associations with the EF Designer, the steps to create a foreign key association are:

  1. Right-click an empty area of the design surface, point to Add New, and select Association….
  2. Fill in the settings for the association in the Add Association dialog.

...being sure to check the Add foreign key properties to the Entity checkbox when filling in the relationship details.

See also: Relationships, Navigation Properties, and Foreign Keys

Shog9
  • 156,901
  • 35
  • 231
  • 235
Mikhail Timofeev
  • 2,169
  • 15
  • 13
  • 5
    And how to do it in EF designer was a link in the page you provided: http://msdn.microsoft.com/en-us/data/jj713299 Thanks! – user2609980 Oct 25 '13 at 15:52
  • 3
    FYI, [this answer is being discussed on meta](http://meta.stackoverflow.com/questions/311753/link-only-answer-not-an-answer-declined) – Shog9 Dec 04 '15 at 20:34
  • If you add a One to One Relationship, the Add foreign key option is grayed out, then what? – Adam Reed Nov 26 '16 at 20:39
5

If you are using the database first approach, it's better to create the foreign key on the database and update the model. Of course, this is true if you can modify the database; if not, you're stuck with the option of creating the referential constraint on the model.

Carles Company
  • 7,118
  • 5
  • 49
  • 75
2

To answer the bonus question:

If you have SQL Management Studio installed then you can very easily use the Database Migration wizard (right click on a database, "Tasks", "Deploy Database to SQL Azure") and from there fill the textboxes with the required information that you get from Azure.

Also, make sure to add your IP to the ignore list in Azure or else you won't be able to upload. This can be done in the Azure webportal.

neo112
  • 1,703
  • 2
  • 17
  • 39