All Articles
Tech hub

Why work with eager loading in Entity Framework Core 6

Lachezar Kamburov
27 Jul 2021
6 min read
Lachezar Kamburov
27 Jul 2021
6 min read

Recently we had an interesting client project that we’d like to share with you in this blog post. We were tasked to build a web application in which the client’s database contained numerous objects with a huge number of navigation properties for each. In addition, there were a lot of nested properties, that would further complicate the process. These two factors represented a tough challenge that we managed to solve.

The web application had to be developed using the .NET stack and more specifically – Progress Kendo UI controls, .NET 5, and Entity Framework Core 6 (EFC6) (See more about ASP.NET MVC Vs ASP.NET Core). Another challenge was that we were provided with a database by the client, so we had to use database-first approach. Our best practice for new projects is to advise our clients to use a code-first approach instead.

How did we handle the above-mentioned challenges related to data loading and eager loading in particular?

Well, let`s go through the 3 ways you can load data from the database in EFC6:

  • Eager loading: A process in which the related data is loaded from the database as part of the initial query.
  • Lazy loading: The related data is transparently loaded from the database when the navigation property is accessed.
  • Explicit loading: The related data is explicitly loaded from the database at a later stage.

After thorough testing and research, the team decided to go with the Eager Loading option implemented in EFC 6.

Why did we choose eager loading?

  1. We needed to send chunks of data (not all objects at once but part of the objects with all their relevant properties loaded) on the load page event.
  2. There were heavily nested properties with depth of around 7 levels/layers.
  3. Entity Framework have a build-in feature to automatically build and manage navigation properties (also known as relationship fix-up) even reuse them if they were loaded before and that without the need to access the database again.

Let’s dive deeper into EFC 6 eager loading

EFC 6 offers us a handy for binding code to database relationships and this is possible by using the “. include” statement at the Data loading layer. Microsoft have provided a perfect example, which you can check here. After going through the example, another of our challenges is described and solved as well - the multiple-level issue. An example of that issue is: Customer contains a list of invoices, and each invoice contains a list of items.

EFC6 solves that by using the “.ThenInclude” method. This is how we managed to achieve building high-performance, durable and stable application using .NET 5 and EFC6.

Lastly, my personal experience

My personal experience in working with EFC6 was very positive as the scaffolding of the database worked perfectly, the generated model was easy to use and understand. The built-in features supported all my needs, giving me the chance to focus more on the front-end part of the project instead of spending more time on database and service layer.

If there’s anything else that comes to your mind as part of a potential solution, or you need help building the right architecture for your data transfers, please, don’t hesitate to get in touch! We’ll be glad to help you identify the critical points and follow the right path through solving the challenge.

.NET
Progress Kendo UI
Entity Framework Core 6

stay tuned

Subscribe to our insights

Secured with ReCAPTCHA. Privacy Policy and Terms of Service.