I have a mental debate with myself every time I start working on a new project and I am designing my POCOs. I have seen many tutorials/code samples that seem to favor foreign key associations:
public class Order { public int ID { get; set; } public int CustomerID { get; set; } // <-- Customer ID ... }
As opposed to independent associations:
public class Order { public int ID { get; set; } public Customer Customer { get; set; } // <-- Customer object ... }
I have worked with NHibernate in the past, and used independent associations, which not only feel more OO, but also (with lazy loading) have the advantage of giving me access to the whole Customer object, instead of just its ID. This allows me to, for example, retrieve an Order instance and then do Order.Customer.FirstName
without having to do a join explicitly, which is extremely convenient.
So to recap, my questions are:
When a variable is associated with an outcome after adjusting for multiple other potential prognostic factors (often after regression analysis), the association is an independent association.
When you change the relationship of the objects attached to the context by using one of the methods described above, Entity Framework needs to keep foreign keys, references, and collections in sync.
A navigation property is an optional property on an entity type that allows for navigation from one end of an association to the other end. Unlike other properties, navigation properties do not carry data.
If you want to take full advantage of ORM you will definitely use Entity reference:
public class Order { public int ID { get; set; } public Customer Customer { get; set; } // <-- Customer object ... }
Once you generate an entity model from a database with FKs it will always generate entity references. If you don't want to use them you must manually modify the EDMX file and add properties representing FKs. At least this was the case in Entity Framework v1 where only Independent associations were allowed.
Entity framework v4 offers a new type of association called Foreign key association. The most obvious difference between the independent and the foreign key association is in Order class:
public class Order { public int ID { get; set; } public int CustomerId { get; set; } // <-- Customer ID public Customer Customer { get; set; } // <-- Customer object ... }
As you can see you have both FK property and entity reference. There are more differences between two types of associations:
Independent association
ObjectStateManager
. It has its own EntityState
!Foreign key association
ObjectStateManager
. Due to that you must follow some special rules.If you want to use foreign key association you must tick Include foreign key columns in the model in Entity Data Model Wizard.
Edit:
I found that the difference between these two types of associations is not very well known so I wrote a short article covering this with more details and my own opinion about this.
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