views:

57

answers:

1

Hi,

I'm probably addressing one of the bigger usability-issues in EF.

I need to perform a calculation on a very big part of a model. For example, say we need a Building, with all of its doors, the categories of those doors. But I'd also need the windows, furniture, roof etc.

And imagine that my logic also depends on more coupled tables behind those categories (subcategories etc.).

We need most of this model at a lot of points in the code, so I'd need to have the whole model filled and linked up by EF.

For doing this, we are simply querying the ObjectContext and using type-safe includes.

But this gets inpractical and error-prone.

Does anyone have suggestions for tackling this kind of problems?

+2  A: 

Use projection to get only the values you need, especially if you don't intend to update everything. You probably don't need every property of a piece of furniture, etc. So instead of retrieving the entity itself, project what you want:

from b in Context.Buildings
where b.Id == 123
select new
{
    Name = b.Name,
    Rooms = from r in b.Rooms
            select new
            {
                XDimension = r.XDimension,
                // etc.

Now you no longer have to worry about whether something is loaded; the stuff you need is loaded, and the stuff you don't need is not. The generated SQL will be dramatically simpler, as well.

Craig Stuntz
thanks for the answer. I am doubting if this approach would be practical, but it looks worth a try...
Bertvan
I find it much more practical than either paying the performance cost of loading stuff you don't need or having to worry about whether the stuff you do need is loaded.
Craig Stuntz