Sponsorship is available! If you’d like to sponsor CodeOpinion.com and have your product or service advertised exclusively (no AdSense) on every post, contact me.If 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 LoadingTo 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.
MigratingAs 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 LoadingI 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.