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

  • Hands On: New VS Code Insiders Build Creates Web Page from Image in Seconds

    New Vision support with GitHub Copilot in the latest Visual Studio Code Insiders build takes a user-supplied mockup image and creates a web page from it in seconds, handling all the HTML and CSS.

  • Naive Bayes Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the naive Bayes regression technique, where the goal is to predict a single numeric value. Compared to other machine learning regression techniques, naive Bayes regression is usually less accurate, but is simple, easy to implement and customize, works on both large and small datasets, is highly interpretable, and doesn't require tuning any hyperparameters.

  • VS Code Copilot Previews New GPT-4o AI Code Completion Model

    The 4o upgrade includes additional training on more than 275,000 high-quality public repositories in over 30 popular programming languages, said Microsoft-owned GitHub, which created the original "AI pair programmer" years ago.

  • Microsoft's Rust Embrace Continues with Azure SDK Beta

    "Rust's strong type system and ownership model help prevent common programming errors such as null pointer dereferencing and buffer overflows, leading to more secure and stable code."

  • Xcode IDE from Microsoft Archrival Apple Gets Copilot AI

    Just after expanding the reach of its Copilot AI coding assistant to the open-source Eclipse IDE, Microsoft showcased how it's going even further, providing details about a preview version for the Xcode IDE from archrival Apple.

Subscribe on YouTube

Upcoming Training Events