Foreign Key relationship in Entity Framework Code First
This is the third part of my Entity Framework Code First Tutorial for beginners. If you want to start fresh , Visit the first part here.
Entity Framework Code First has some nice conventions to follow when you want to work with relationships. Let’s see how it works.
If your class contains a reference and a collection nvigation property, EF will create a One-tp-Many relationshop. The navigation property need to be at only side(class) to get this one to many relation ship to be working.
Let’t take the customer class from our part 2 source code. We will create a new model class for Address.
Now we will add a Navigational property to our Customer class. Since one customer can have multiple Addresses, We can add a Collection property.
Now, Entity framework CodeFirst is going to create a new table for me to store the Addresses and it will have a Column to store the CustomerID. The convention for the name of the column is the “PrimaryKeyTableName_PrimaryKeyNameOfPrimaryKeyTable“.
The output is
Now We can fill the collection property to save the Addresses while creating a new customer. Let’s update the HttpPost Create action method like this.
We can also use Data Annotations also to define the Relationship, if you are not following the Convention (naming of the properties).I may explain it later in a different post.





July 16th, 2012 at 5:54 am
Thanks for replying. But I have more queries from you.
1) Does navigation property automatically sets the foreign key relationships in the database? Like if I Keep a list of Address in the Customer class than the relation would be one-to-many from Customer to Address in the database.
2) If It does not create automatically than how can I set the foreign key relationship usign modelbuilder class.
Thanks in advance
July 16th, 2012 at 12:46 pm
@Forhad :
1) Yes. It will automatically be set if we follow the naming conventions of the properties.( if i use the List
class in Customer , It will be one to many (one customer many address))2) We can alternatively use modelBuilder to set it with Fluent API also.
July 16th, 2012 at 3:54 pm
OK Thanks a lot.
1)But what about many to many?
Should I keep List in both classes Like Products should have a list of its Buyers and Buyers should have a list of Products.
Than automatically we can get many to many relationship among them.
Thanks in advance @Shyju
July 16th, 2012 at 4:02 pm
@Forhad : Yes. Keep collection property as navigation proprty in both classes. Keep it virtual using the virtual keyword
July 16th, 2012 at 4:29 pm
Thanks man
You have been very helpfull
December 20th, 2012 at 5:37 am
Hi,
This is working while inserting a new customer and collection of address but It is not working while updating the address.
Say First time i have created Customer id 1 with 2 addresses as Address 1 and Address 2
How to update the address?
December 20th, 2012 at 5:59 am
@muthuvel: You need to load the address entity back from the db and then change the property values you want to updates (Ex:City) and Submit the changes back to the dbcontext.