February 27, 2014

To continue on from the previous post, in this post I will show how to create a relationship between two entities in the model. In the last post, I had an Employee entity created. In this one, I will create a new entity Department, update the Employee entity to have an association with Department.

Here is how the model looks like currently.

 image

Now, the Department entity is added in the same way Employee was added previously. The model looks like below now.

image

Now, to add the association, right click on Employee entity and choose Add New > Association.

image

An Add Association prompt like below will appear with most of the options correctly set.

image

Notice that Multiplicity has been incorrectly set in this scenario. I will invert the relationships so 1 department can have multiple employees. So , this will look like below.

image

Notice that there is an option to “Add Foreign key to the Employee Entity”. I have selected it clicked OK. The model is now updated but the foreign key is named as “DepartmentDepartmentId” like below. This happened as I renamed the original primary key from “Id” to “DepartmentId”.

image

I have renamed it to DepartmentId by clicking on it and editing it through the properties. My model is now ready and I clicked Validate like but it failed with the messages below.

image

This is because the new table does not exist yet. So, I will right click and choose generate database from model which will correct this issue as shown in the previous post.

That’s it, the association is now created and the model is good to go.

0 comments:

Reference: Shahed Kazi at AspNetify.com