EF Core: Logging Lazy Loading

Entity Framework Core: Logging Lazy LoadingIf you are planning on migrating from Entity Framework 6.x to Entity Framework Core, there is likely one major road block: Lazy Loading.

In most circumstances I don’t think lazy loading is a good idea.  If you were using Entity Framework, you may be using Lazy Loading unintentionally since it’s built in by default.

Eager Loading

To eager load a related entity, you specify the related entity to be populated with the Include() method.  This is applicable in EF and EF Core.  However in Entity Framework 6.x, if you do not Include() a related entity, and access that navigation property, but default it will be lazy loaded.

Meaning a SQL statement will occur to load the related entity.  If you are iterating over a collection, this is where the N+1 comes from.

I assume this is why Entity Framework Core held out until 2.1 before providing a way to lazy load related entities since it’s now an opt-in.


As mentioned, Entity Framework Core 2.1 now supports Lazy Loading as an opt-in.

I don’t recommend turning this on from a fresh project, but it may be applicable if you are migrating from Entity Framework 6.x to EF Core.   You can get pretty far with your existing LINQ queries without having to rewrite a large portion of your app.

Enable Lazy Loading

I don’t want to re-write the docs on how to enable Lazy Loading, so check them out.  However, I do want to show my preferred method and strategy that if you enable lazy loading, that you can log when it occurs.

This will provide you insights about where in your existing LINQ you are not calling Include() and eager loading the related entity.

First, to enable lazy loading, I’m going to add a param of Action<object, string> that will be injected into your entity by Entity Framework.  If the related entity being accessed is null, I’m going to Load the related entity, but also log out to the Console.  Obviously you would be using the logging tool you prefer, for this demo I’m just creating a console application.


Here’s a simple console application that first inserts a parent entity with two children. Then on the first call it does not Include() and will force lazy loading, which in turn will be logged to the console.  In the second call, Include() will be called and no lazy loading occurs.

Migration Comments

Have you migrated from Entity Framework 6.x to Entity Framework Core?  I’d love to hear about it in the comments or on Twitter.

Enjoy this post? Subscribe!

Subscribe to our weekly Newsletter and stay tuned.

Roundup #3 – VS Live Share, ML.NET, Desktop .NET Core, EF6.3 on .NETCore3, Hyper-V Android Emulator

Since this week was Microsoft Build 2018, these were the top 5 things that came out of it for me.  Obviously I’m leaning towards the topics that affect me the most.  I’d love to hear what you found most interesting.  Let me know in the comments or on Twitter.


Visual Studio Live Share

If you work remotely or want to screen share with a co-worker in another office, you know how terrible most of current solutions are.  VS Live share was announced in preview back in November 2017 and is now available.

It doesn’t matter what type of app you’re building, what language you’re programming in, or what OS you’re on: when you need to collaborate, Live Share will instantly share your project with a teammate. Edit and debug together in real time without teammates needing to clone a repo or set up their environment.

Link: https://www.visualstudio.com/services/live-share/


.NET Core 3 to support WinForm, XAML & UWP

At Microsoft Build Live today, we are sharing a first look at our plans for .NET Core 3. The highlight of .NET Core 3 is support for Windows desktop applications, specifically Windows Forms, Windows Presentation Framework (WPF), and UWP XAML. You will be able to run new and existing Windows desktop applications on .NET Core and enjoy all the benefits that .NET Core has to offer.

Link: https://blogs.msdn.microsoft.com/dotnet/2018/05/07/net-core-3-and-support-for-windows-desktop-applications/


Introducing ML.NET: Cross-platform, Proven and Open Source Machine Learning Framework

Today at //Build 2018, we are excited to announce the preview of ML.NET, a cross-platform, open source machine learning framework. ML.NET will allow .NET developers to develop their own models and infuse custom ML into their applications without prior expertise in developing or tuning machine learning models.

ML.NET was originally developed in Microsoft Research and evolved into a significant framework over the last decade; it is used across many product groups in Microsoft like Windows, Bing, Azure, and more .

Link: https://blogs.msdn.microsoft.com/dotnet/2018/05/07/introducing-ml-net-cross-platform-proven-and-open-source-machine-learning-framework/


Entity Framework 6.3 on .NET Core 3

At Microsoft Build 2018 they also announced that Entity Framework 6.3 will run on .NET Core 3.  This is pretty big news if you want to migrate to .NET Core but have giant road block of Entity Framework and can’t easily move to EF Core.

Hyper-V Android emulator support

Today, at Build 2018, we announced a preview of the Google Android emulator that’s compatible with Hyper-V, available on the Windows 10 April 2018 Update. This enables developers with Hyper-V enabled on their machines to use a hardware accelerated Android emulator, without needing to switch to Intel’s HAXM hypervisor. Amazing work was done by the Windows Hyper-V team, with help from the Xamarin team, to make to this happen.

Link: https://blogs.msdn.microsoft.com/visualstudio/2018/05/08/hyper-v-android-emulator-support/


Enjoy this post? Subscribe!

Subscribe to our weekly Newsletter and stay tuned.

Roundup #2

dotMemory Unit

dotMemory Unit is a unit testing framework which allows you to write tests that check your code for all kinds of memory issues. You can now extend NUnit, MSTest or another .NET unit testing framework with the functionality of a memory profiler.

Perfect fit for any workflow: integrated with Visual Studio, works with stand-alone unit test runners, Continuous Integration ready. Last but not least, dotMemory Unit is free.

This week I was running into some memory issues with 3rd party library.  I just stumbled up on dotMemory Unit and proved to be really useful to verify no memory leaks.

Link: https://www.jetbrains.com/dotmemory/unit/

Directory.Build.targets Example

This repository shows an example of controlling NuGet package versions at the repository level, while at the same time ensuring that projects only opt into packages that they want.

This is fantastic.  Ability to specify all the NuGet packages and the version that your solution projects can opt-in without having to worry about the version per project.  This is especially useful when you have a solution with multiple projects that all need to reference the same NuGet package.  This allows you to keep them in sync without having to worry keeping the versions in-sync across multiple csproj and <PackageRefernece>.  Lovely.

Link: https://github.com/davkean/maket


WinDbg Preview

We’ve updated WinDbg to have more modern visuals, faster windows, a full-fledged scripting experience, and Time Travel Debugging, all with the easily extensible debugger data model front and center. WinDbg Preview is using the same underlying engine as WinDbg today, so all the commands, extensions, and workflows you’re used to will still work as they did before.

I blogged about Troubleshoting Stackoverflow Exceptions, and WinDbg came in really handy.  I first used the original version and then realized there is a new WinDbg in the Windows Store that has a much needed UI refresh.

Link: https://www.microsoft.com/en-us/store/p/windbg-preview/9pgjgd53tn86


Indu Alagarsamy – Autonomy & Asynchrony: The Key To Designing Reliable Systems

I was lucky enough to present at this conference in 2017.  I thought this was a great presentation and refer people to watch it all the time.  Highly recommend if you are interested in Event Driven Architecture.

Explore DDD 2017 – Denver, Sept. 21-22 The real world, with all its complexity, can at the same time be simple, elegant and beautiful. It thrives on autonomy and asynchrony, the two most important things that bring order to chaos. The real world does not pause for something to complete before moving on. And yet, when we write software, “Command and Control” is the norm. We find clever ways of doing this while keeping up with all the new technologies and the languages in fashion, all the while ignoring the realities of life. In this session, explore an alternate universe in which Event Driven Architecture can power even the most complex mission-critical systems. Learn how communicating asynchronously via events leads to building systems that are autonomous and much more reliable. Embrace asynchrony and autonomy. Make the complex simple.

About Indu Indu Alagarsamy has been programming for over 15 years. She is currently part of the development team at Particular Software, the makers of NServiceBus, practicing her passion. Indu is also passionate about diversity and inclusiveness in the tech industry. When not programming, she is either rock climbing in sunny Southern California or having fun with her kids!

Link: https://www.youtube.com/watch?v=v-7RevY6No4


.NET Framework 4.7.2 Release Notes

If you’re running full framework, you might want to check out the release notes to see if there are any improvements for your workloads.  Quite a few BCL and CLR bug fixes.

Link: https://github.com/Microsoft/dotnet/blob/master/releases/net472/README.md


If you have any content you want me to take a look at to include in my next Roundup, let me know in Twitter or in the comments.  If you enjoyed this roundup, I plan on emailing them out weekly after posting.  Sign up below!

Enjoy this post? Subscribe!

Subscribe to our weekly Newsletter and stay tuned.