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)
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With