Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreign Key vs. Independent Relationships - is there improvement with Entity Framework 5?

I have read several articles and questions on concept of foreign key vs independent relationship when using Entity Framework. And I am still not 100% sure which way to go.... I would prefer not to "pollute" my domain POCOs by having a property that will be used in FK relationship when I already have a property reference to "has a" object.

My questions are (looking at you @EFTeam, @Ladislav Mrnka)

  1. are there any improvements on this subject in the upcoming Entity Framework v5?
  2. are there more advantages if I use FK instead of independent associations (particularly with code first)?
like image 239
zam6ak Avatar asked Jun 19 '12 23:06

zam6ak


People also ask

Do we use foreign keys in Entity Framework?

The ForeignKey attribute is used to configure a foreign key in the relationship between two entities in EF 6 and EF Core. It overrides the default conventions. As per the default convention, EF makes a property as foreign key property when its name matches with the primary key property of a related entity.

What is difference between Entity Framework 5 and 6?

EF5 is built into the core of . NET 4.5, whereas EF6 has been shifted out, and is open source. This means that you must add the new EF6 assemblies to all of the relevant projects in the solution, in particular the entry project. This means that you must remove assembly System.

How does Entity Framework handle many-to-many relationships?

To configure many-to-many relationship Using Data Annotations, you need to create the Join Table in the model. The Join Table BookCategory will have properties for the primary key of both the table. It will have two navigational properties one each for Book and Category class.

How will you create relationship between tables in Entity Framework?

In relational databases, relationship is a situation that exists between relational database tables through foreign keys. A Foreign Key (FK) is a column or combination of columns that is used to establish and enforce a link between the data in two tables.


2 Answers

If you have a large model, you're bound to "pollute" your domain objects (or conceptual model in general) anyway. For models with FK-mapped associations, the cost of "view generation" - a stage in the EF processing pipeline necessary to execute queries or save changes, one that can be moved to build-time ("pre-generating views") - is lower compared to models with independent associations. This is important because the amount of time to perform it may be unnoticeable for small models, but it gets longer very fast, especially when there are associations mapped to nullable foreign keys (to-0..1, or to-1 with derived entities in TPH mapped hierarchies). In the official EF5 performance considerations document an example difference in view generation time for a very large model is given as between "over a month and then we gave up" (with independent associations) and 104 minutes (with FK-mapped associations). In my case (several hundred highly connected entities) it's between 25 minutes and 40 seconds. The situation is the same in EF5 as it was in the previous versions.

like image 59
cynic Avatar answered Oct 12 '22 15:10

cynic


There are no improvements. Both options still exist. Advantage of using FK association is simplified handling of changes in one-to-many relation when working with disconnected object graphs. It should also simplify data binding if you use it.

like image 22
Ladislav Mrnka Avatar answered Oct 12 '22 17:10

Ladislav Mrnka