NashTech Blog

Entity Framework Core Internals: High-Level Overview

Table of Contents

Introduction

Have you ever wondered what and how Entity Framework Core (EF Core) does behind the scenes after you instantiate a DbContext and use it to execute a LINQ query? You might have known or heard that people usually say that it will translate your query into something that your target database can understand to execute and return the results, this looks very simple at first glance but what really happens behind is more complex than that. If you are curious like I am, this article should be right for you.

What happened when your 1st query was executed?

The below image illustrates the process of how a LINQ query is executed at a very high level:

Let’s take the below code as an example:

After the code at line number 3 is executed, only the step DbContext Instantiation is executed as DbContext is lazy, it will not do heavy stuff until you actually start using it.

Let’s continue our code execution, at line number 5, we start using the DbContext then a lot of things are going to happen.

Continue code execution, after ToList() is invoked, Compile Query and Execute Query take place too.

What happened when your 2nd, 3rd, … queries were executed?

Now that you see, EF Core does a lot of things in order to serve your query, and you might think that’s why it is slower than Dapper or ADO, luckily isn’t that slow because EF Core is smart enough to leverage caching a lot, so for the subsequent queries it will not need to do all the heavy steps again and again.

Let’s refactor our code a bit to make the using scope more explicit:

Duplicate the using block to make use of the DbContext the 2nd time:

Let’s debug our code again, stop at line number 8, at this time because the code at line 5 was executed, so there should be a lot of stuff in the cache right now.

Continue stepping and stop at line number 9, the DbContext Instantiation step took place, however, the same work was done for the same DbContext type previously and put in the cache, this time it was run very quickly.

Let’s continue our code execution, at line number 10, we start using the DbContext then a lot of things are going to happen similar to line number 5 but this time the Internal Service Provider and the Model were already in the cache and the execution time should be much faster.

Continue code execution, after ToList() is invoked, Compile Query and Execute Query take place and of course, the cache is leveraged too.

Summary

In this article, we took a quick look at a very high-level overview of how EF Core query execution works under the hood, in future articles we will take a deep dive into the architectural design and source code to see how each step in the pipeline works.

The diagrams in this article were drawn using draw.io, in case you are interested please get the diagrams file here.

Picture of Phong Nguyen

Phong Nguyen

Phong is currently working as Technical Architect at NashTech, has over 12+ years of experience in designing, building and integrating Enterprise Applications. He is interested in Performance Optimization, Security, Code Analysis, Architecture and Cloud Computing.

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

Scroll to Top