Tuesday, December 1, 2009

Entity Framework 4.0 - Foreign Key Relationships

The current version of EF (v3.5) maps table relationships as properties, hiding database foreign-key columns. However, there are some scenarios where you don’t need the entire referenced object to do some operation, you only need its “Id” (foreign key).

For example, in a web application it is very common to use combo-boxes in order to assign relationships. In these scenarios, after a post-back it is necessary to re-create the model and its relationships (using combo-boxes “selected values”). Since you don’t have the referenced objects, you must get them from the database or use “Stub Entities” to simulate them.

To solve this problem, the version 4.0 will also allow you to map foreign-key columns as properties, making it easier.

More info: http://blogs.msdn.com/adonet/archive/2009/11/06/foreign-key-relationships-in-the-entity-framework.aspx.

No comments: