Site icon Chris Woody Woodruff

Compiled Models: The Fast Lane for EF Core Performance

Compiled Models: The Fast Lane for EF Core Performance

Let’s talk about startup time. Not the Silicon Valley kind, but the time it takes for your EF Core app to boot up, stretch its legs, and actually start handling requests. If your app is dragging its feet like a teenager on a Monday morning, you need a secret weapon: Compiled Models.

Think of compiled models as pre-built blueprints for your database schema that EF Core can load instantly, skipping all the tedious setup. It’s like meal-prepping your database models so your app can hit the ground running.


What Are Compiled Models, Anyway?

Typically, EF Core builds your model on the fly every time your app starts up. It figures out your entities, relationships, and configurations—basically, everything you’ve already told it in code. While it works, it’s not exactly speedy, especially for large or complex models.

Compiled Models take that runtime work and shift it to build time. The result? EF Core already knows everything it needs, so it can skip the warm-up routine and start doing what it does best: handling your data like a champ.


How Much Faster Are We Talking?

Here’s a quick comparison:

And when your app is deployed across multiple instances (hello, microservices!), those time savings really add up.


Setting Up Compiled Models

Ready to give compiled models a spin? Here’s how to set it up in your EF Core project:

Install the Necessary Tools

You’ll need the EF Core CLI tools. If you don’t have them yet, install them with:

dotnet tool install --global dotnet-ef

Generate the Compiled Model

Run the following command in your project directory:

dotnet ef dbcontext optimize --output-dir Models --namespace MyApp.Models

This creates a compiled model in the Models folder with all the database schema goodness baked in.

Use the Compiled Model in Your DbContext

Update your DbContext to load the compiled model:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder
        .UseModel(MyApp.Models.MyCompiledModel.Instance)
        .UseSqlServer("<YourConnectionString>");
}

That’s it! Your app is now cruising in the fast lane.


The Fine Print

Compiled models are amazing, but they come with a few “house rules”:


Why Use Compiled Models?

Here’s why compiled models are a game-changer:


Wrap-Up: Build It Once, Use It Always

Compiled Models are like meal-prepping for your EF Core app—put in a little effort up front, and you’ll save time every time your app starts up. Whether optimizing for the cloud or just looking to shave some seconds off your app’s startup, this tool is worth adding to your kit.

So, grab your EF Core CLI and start compiling. Your app (and your users) will feel the difference.

Exit mobile version