views:

86

answers:

1

With EF4 comes a new feature "Include foreign key columns in the model". What was the motivation behind adding this feature, and are there any particular gotcha's with either leaving it off, or turning it on when generating a model from an exisiting database?

+1  A: 

Without knowing the true motivation for it, I think The value of having these additional columns comes when you're exposing your Entities over Web Services or directly to the presentation tier, which BTW I don't personally do or recommend.

Consider the following examples.

If you didn't have the foreign key properties then your client code would look like below. You most likely wouldn't want to load an instance of the Category from the datastore so you end up creating a new instance of the Category with only the Id set just to represent the FK relationship.

        var product = new Product
        {
           Category = new Category {Id = 1},
           Name = "Product 1"
        };

In the previous version of Entity Framework it was even more messy and you ended up with code like this.

        var product = new Product();
        product.Name = "Product 1";
        product.CategoryReference = new EntityReference<Category>();
        product.CategoryReference.SetEntityKey<Category>(1); 

Although it may not be pure I personally think this is much easier to read and less work for the client.

        var product = new Product
        {
            CategoryId = 1,
            Name = "Product 1"
        };

Pros

Easier coding experience for clients working with your Entities.

Cons

Not entirely pure from a DDD perspective to have two properties on your Entity.

willbt