Practical .NET

Mimic Lazy Loading with Entity Framework 6 and Entity Framework Core 1.1

Entity Framework Core doesn't have lazy loading (at least, not yet). But you can fake it by using explicit loading, though it doesn't work quite the way you might want. In fact, it's probably a good idea to use this in Entity Framework 6.

In an earlier column, I took a first look at what Entity Framework (EF) Core 1.0/1.1 does (and doesn't) do compared to EF6. One of the features that EF Core doesn't include is lazy loading. In EF6, thanks to lazy loading, if you touch a navigation property whose objects haven't yet been retrieved, Entity Framework automatically issues a call back to the database server to fetch those objects for you.

I'm not a big fan of lazy loading because, in most scenarios, I don't think it makes sense (I discussed when lazy loading might make sense in another column). But that doesn't mean you won't need it … or something like it when working with EF Core.

That "or something like it" is what the EF Core team calls "explicit loading," which is also available in EF6. Explicit loading also allows you to issue a query back to the database server to retrieve the objects in a navigation property. The reason it's called "explicit" is because, unlike lazy loading, you have to add a line of code to your application to trigger the query.

Here's some EF code that retrieves a collection of Customer objects and then, in the last line, uses explicit loading to populate the Customer object's Salesorders property:

using (db = new CustomerOrderContext())
{
  var custs = from cust in db.Customers
              where cust.Salesorders.Count() > 0
              select cust;
  foreach (Customer ct in custs)
  { 
    db.Entry(ct).Collection(cst => cst.Salesorders).Load();

Unfortunately, as written, this code won't work. When you get to the explicit loading line, you'll get an error that there already exists an Open DataReader on the connection and the query to retrieve your Salesorders won't be processed.

The workaround is to fully populate the custs collection by calling the ToList method on the query. Here's the code, revised:

using (db = new CustomerOrderContext())
{
  var custs = (from cust in db.Customers
               where cust.Salesorders.Count() > 0
               select cust).ToList();
  foreach (Customer ct in custs)
  { 
    db.Entry(ct).Collection(cst => cst.Salesorders).Load();
ToList both opens and closes the connection to the database (while retrieving your objects in between, of course), freeing up your connection to retrieve Salesorders later in your code.
comments powered by Disqus

Featured

  • IDE Irony: Coding Errors Cause 'Critical' Vulnerability in Visual Studio

    In a larger-than-normal Patch Tuesday, Microsoft warned of a "critical" vulnerability in Visual Studio that should be fixed immediately if automatic patching isn't enabled, ironically caused by coding errors.

  • Building Blazor Applications

    A trio of Blazor experts will conduct a full-day workshop for devs to learn everything about the tech a a March developer conference in Las Vegas keynoted by Microsoft execs and featuring many Microsoft devs.

  • Gradient Boosting Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the gradient boosting regression technique, where the goal is to predict a single numeric value. Compared to existing library implementations of gradient boosting regression, a from-scratch implementation allows much easier customization and integration with other .NET systems.

  • Microsoft Execs to Tackle AI and Cloud in Dev Conference Keynotes

    AI unsurprisingly is all over keynotes that Microsoft execs will helm to kick off the Visual Studio Live! developer conference in Las Vegas, March 10-14, which the company described as "a must-attend event."

  • Copilot Agentic AI Dev Environment Opens Up to All

    Microsoft removed waitlist restrictions for some of its most advanced GenAI tech, Copilot Workspace, recently made available as a technical preview.

Subscribe on YouTube