benjy
benjy

Reputation: 4716

DataLoadOptions equivalent for LINQ to Entities?

Is there a version of the DataLoadOptions class that exists in LINQ to SQL for LINQ to Entities? Basically I want to have one place that stores all of the eager loading configuration and not have to add .Include() calls to all of my LINQ to Entities queries. Or if someone has a better solution definitely open to that as well.

TIA,
Benjy

Upvotes: 5

Views: 3834

Answers (2)

Gert Arnold
Gert Arnold

Reputation: 109080

Personally I'm glad that there is no (official) EF equivalent of DataLoadOptions. Why? A few reasons:

  • It is too easy to use them in a way that exceptions are thrown, see the remarks section of the MSDN page.
  • I don't like the concept of filtered child collections. When a Customer has Orders, I want the Orders member to represent the orders of that customer (lazy or not). A filter defined somewhere else (by AssociateWith) is easily forgotten. I will filter them when and where needed. This leads to the last objection:
  • Most importantly: it is stateful and most bugs are caused by unexpected state. DataLoadOptions change the DataContext's state in a way that later queries are influenced. I prefer to define eager loading where and when I need it. Typing is cheap, bugs are expensive.

Nevertheless, for completeness's sake I should mention that Muhammad Mosa did put some effort in an EF version of DataLoadOptions. I never tried it though.

I realize that you probably want to prevent repetitive code. But if you need identically shaped queries in more than one place you are already repeating code, with or without "globally" defined includes. A central eager loading configuration is pseudo DRY-ness. And soon you'll find yourself tripping over your own feet when eager loading is not desired but, darn, it's configured to happen!

Upvotes: 5

Vitalij B.
Vitalij B.

Reputation: 415

Entity Framework does not support eager loading settings for the whole 'ObjectContext'. But you can declare all required 'IQueryable' properties with include options in a partial class. For example:

public IQueryable<Order> Orders {
  get {
    return OrderSet.Include("OrderDetails");
  }
}

Upvotes: 5

Related Questions