Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 599
benjy Avatar asked Aug 25 '12 16:08

benjy


People also ask

Can we use LINQ with Entity Framework?

LINQ to Entities provides Language-Integrated Query (LINQ) support that enables developers to write queries against the Entity Framework conceptual model using Visual Basic or Visual C#. Queries against the Entity Framework are represented by command tree queries, which execute against the object context.

How to achieve Lazy Loading in LINQ?

Lazy Loading means the related entities are not loaded, until we iterate through them or bind them the data. By default, LINQ to SQL loads the related entities, using Lazy Loading. There is one-to-many relationship between Department and Employees entities. A Department can have 1 or more employees.

What is include in LINQ to Entity?

LINQ Include allows retrieving the related entities to be read from database in same query. By using the Include method we can easily read all related entities from the database in a single query.

What is the difference between EF and LINQ?

Entity Framework is an object-relational mapping (ORM) framework for connecting C# code to external databases, usually SQL Server. LINQ is a query language embedded into C# and a set of extension methods in order to make it useful.


2 Answers

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!

like image 194
Gert Arnold Avatar answered Sep 28 '22 08:09

Gert Arnold


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");
  }
}
like image 27
Vitalij B. Avatar answered Sep 28 '22 06:09

Vitalij B.