Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add foreign key relationship in Entity Framework?

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

like image 753
Erwin Rooijakkers Avatar asked Oct 25 '13 13:10

Erwin Rooijakkers


People also ask

Does Entity Framework have a foreign key?

The Entity Framework Core Fluent API HasForeignKey method is used to specify which property is the foreign key in a relationship. In the following example, the AuthorFK property in the Book entity does not follow Entity Framework Core's convention for foreign key names.

How do I set a foreign key in EF core?

If you want the foreign key to reference a property other than the primary key, you can use the Fluent API to configure the principal key property for the relationship. The property that you configure as the principal key will automatically be set up as an alternate key.

How do I create a foreign key in Entity Framework Code First MVC?

To create Foreign Key, you need to use ForeignKey attribute with specifying the name of the property as parameter. You also need to specify the name of the table which is going to participate in relationship. I mean to say, define the foreign key table.


3 Answers

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

like image 116
Mikhail Timofeev Avatar answered Nov 09 '22 00:11

Mikhail Timofeev


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.

like image 45
Carles Company Avatar answered Nov 09 '22 02:11

Carles Company


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.

like image 20
neo112 Avatar answered Nov 09 '22 00:11

neo112