Improve application startup time with EF Core compiled models
This post is about improving EF Core performance with compiled models. EF Core compiled models feature introduced in EF Core 6.0 which will provide both better startup performance, as well as generally better performance when accessing the model. This feature is very useful when you’re using very large models with relationships.
When you’re running an ASP.NET Core app with EF Core - if your DbContext has a lot of entities and relationships it will take some time to load first time. The subsequent calls will use the cached version, still it will impact the application startup time. For compiling the model, we need to use the EF Core CLI command. This command optimizes the Build model step in the EF Core query execution steps.
Here are the steps of running an EF Core query
- Db Context initialization.
- Build internal service provider.
- Build model.
- Compile query
- Execute query.
We can use the
dotnet ef dbcontext optimize -c FeedbacksDbContext -o .\Data\DbContextOptimized -n Feedbacks.Data.Optimized command to generate the compiled models. This command will generate the compiled models inside the
Data\DbContextOptimized folder, under
And then you need to use the compiled models in the program.cs -
AddDbContext method like this.
When you’re using compiled models, make sure you’re updating the compiled models when you’re changing the model classes. Also note, some features like Global query filters, Lazy loading proxies are not supported. You can find more details about the Compiled Models here - Announcing Entity Framework Core 6.0 Preview 5: Compiled Models
You include the model compilation as part of your build pipeline - here is the github action which will compile the model if any classes inside the models folder got changed.
paths-filter task will check whether any changes happened in the models folder, and if yes, it will set the
steps.filter.outputs.models’s value to true, and Github action will execute the compile models step.
This way using Compiled models feature you can improve the application startup and query performance. And it will be easy to include it in the build pipeline so that you won’t forget to run the command if model got changed.