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

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube