Upgrading Independent Association to Foreign Key Association
In EF when you want to define a relationship between two entities, you typically use an association. An association can be many-to-many, 1.. 0..1, many-to-1 etc. Typically you would have tables in the database which are related to each other using foreign key constraints. When you import those tables in Entity Framework, EF would create an association.
In v1 of EF, there is only 1 type of association called Independent Association. Independent association is an association which is tracked separately from the entity itself. To better explain, I think I will start with basic EF model that contains Product and its related Category as shown below.
In the above model, Product has (*.. 0..1) Independent Association with Category. Well you might ask, why are you calling this association as independent association or how can u tell if this is an Independent Association? You can tell this association is Independent Association because Product entity does not have CategoryId property exposed on it. The only way for you to know what is the Category of a product is by using its navigation property like
product.Category.CategoryId.
In Independent Association, foreign key column is never exposed. It is based on the concept that foreign key columns are database concepts and such column should never surface on the model. Whenever you want to deal with a foreign key column you would use the navigation property on the conceptual model to move from entity to another or even change its relationship. Although Independent association makes your model look very clean, it comes with lot of draw backs that you will face once you start using it. Among the many issues, the most noticeable one is the binding. Most of us like to bind an entity to listview or gridview control or bind a selected value in the dropdown to be a foreign key column on the entity. You cannot do that with Independent association because there is no foreign key column. For instance if you want to set the selected value of a category dropdown list to be the categoryId of a given product you would do something like this.
dropdown.SelectedValue = product.CategoryId;
Now this is very simple operation you want to do but with Independent association you have to either load the entire category object or use entityreference to get access to the categoryid of a given product. In short it can be achieved but not so easily. Then comes another drawback which is related to the query EF generates when you a fetch an entity that has an independent association to other entities. There are more issues with Independent association which I don’t want to talk about because that is not the subject of this blog post.
In EF4, Entity Framework team introduced Foreign Key Association. As the name says, this association is both represented as a navigation property and a foreign key column. I love foreign key association because it solved all the problems and complains that many developers had with Independent Association. In this blog post I will show you the steps to upgrade Independent Association to foreign key Association. Steps are show below.
- On the Product entity add a new property called CategoryID with Integer data type and make it nullable. The reason it is nullable is because on the database we have made CategoryId foreign key column as allow nulls.
- ON the Product entity table Mapping, map the new CategoryID property to CategoryID column on the Products table as shown below.
- Select the Association between Product and Category entity and on the properties window of that association, select Referential Constraint.. This will open up Referential Constraint dialog. On the Principal entity, select Category. Once set, EF will automatically populate the Principal and Dependent key. Figure below shows the values defined for Referential Constraint Dialog.
- Since Foreign key associations are not tracked separately from the entity, we need remove the mapping that was defined for independent association. When you select the association on the model, on the mapping Details window on the bottom you would see a line that says delete these mapping. Figure below shows the mapping detail screen shot for the association.
Click delete these mapping to delete the mapping. Build the project and make sure there are no warnings.
You have now upgraded the association to Foreign Key association.