If you don’t know what eager loading is, Jump to “What’s eager loading?”.
Eager Loading Syntax
If you are eager loading Products for example in a typical (Categories 1<->* Products) relation, the standard syntax would like:
DbDataContext.Categories.Include(“Products”)
What is the problem with that?
The “Products” part. The word “Products” is a string. If I rename the Products table to ShopProducts or whatever or even remove it from this data diagram and have it elsewhere, or even something wrong happens and the relation is removed from DB/diagram by mistake, my code will still compile, but will fail when it runs. This is BAD BAD BAD.
How to solve this?
Since I always believe that if something exists somewhere you shouldn’t do it yourself unless its totally broken (and I mean REALLY REALLY BROKEN), I started searching inside the Entity Framework itself for something to get the entity name from.
At first it seemed super easy. Every entity class has a static property “EntityKeyPropertyName”, so, I thought I can write something like:
DbDataContext.Categories.Include(Product.EntityKeyPropertyName); // But this didn’t work
CV Download (.docx)
Google Reader Shared Items
Twitter Updates