Title: EF Core 9 Migrations Extremely Slow and Causing System Freezes
I'm experiencing significant performance issues when working with Entity Framework Core 9 migrations. Specifically:
Project Details:
ASP.NET MVC project targeting .NET 9
Using EF Core 9 with Microsoft.EntityFrameworkCore.SqlServer
Approximately 50 entities with simple relationships
Around 20 existing migration files
Issue Description:
Building the project takes about 30 seconds, which is acceptable.
However, adding or updating migrations causes the system to freeze and can take up to 5 minutes or more.
During this time, CPU usage spikes, and the system becomes unresponsive.
This issue significantly hampers productivity, especially when needing to create multiple migrations.
Troubleshooting Steps Taken:
Verified that the dotnet-ef tool version matches the EF Core version.
Attempted to consolidate migrations to reduce their number.
Reviewed entity models to simplify complex relationships.
Monitored system resources to identify potential bottlenecks.
Despite these efforts, the problem persists. I came across a similar issue reported here: EF9 Migration Performance Regression #35716, but it appears to be closed without a resolution.
Request:
Please investigate this performance regression in EF Core 9 migrations. If additional information or a minimal reproducible example is needed, I'm willing to provide it.
This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.
This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.
I’m not sure I can create a small runnable project to reproduce this issue, as it’s not tied to a specific query or function. The freezing happens at the solution level when creating or updating migrations; the entire process causes the build and system to hang, even on a high-performance machine.
If there were a diagnostic or profiling feature available that could capture logs during migration operations, I’d be happy to use it and share the results. That kind of tooling would really help in cases like this where the issue is tied to overall project size or structure rather than isolated code.
How big is your total project size (in terrms of megabytes), and how big are individual migrations in the project?
How big is your total project size (in terrms of megabytes), and how big are individual migrations in the project?
The repository folder size is approximately 2.1 GB. It includes the .sln file, the main MVC startup project, and a separate class library for the entity models and DbContext.
As for the individual migrations, they are very small; in fact, even creating an empty migration causes the same issue. The problem consistently occurs during the migration process (create or applying a migration), freezing the build regardless of the migration content.
The repository folder size is approximately 2.1 GB
I'm assuming that excludes any .git size - where does the size come from, which files in your project?
The repository folder size is approximately 2.1 GB
I'm assuming that excludes any .git size - where does the size come from, which files in your project?
Ah, yes; sorry about that. I’ve now excluded the .git folder and other similar directories, including the bin and obj folders, which were particularly large in the MVC project. After cleaning those up, I can say that the actual code-related files are around 800 MB in size.
And as I asked above, which code-related files actually make up 800MB in size? This is very much an abnormally large size for most coding projects, and in general will not perform well in various scenarios.
And as I asked above, which code-related files actually make up 800MB in size? This is very much an abnormally large size for most coding projects, and in general will not perform well in various scenarios.
There are a some document templates files in one of the projects that are marked as embedded resources, but they’re not related to the actual codebase. Also, the wwwroot folder contains many front-end libraries, which are unrelated to our C# code. For reference, the Migrations folder itself is about 137 MB.
OK.
First, I'm not sure 800MB for actual code is a very large number; I'm not sure what you mean by "front-end libraries" inside wwwroot, and whether these are part of what's compiled; either they are (and they matter), or they're not (in which case they're not code-related).
In any case, even 137MB for migrations is a huge amount of code, which will cause your project to suffer from potentially severe performance issues. I'm not sure how you've managed to get to 137MB - using HasData to seed large data is a frequent culprit (see the docs on this). To find out whether the migration size is indeed the problem, you can temporarily delete that folder, and then perform several operations (such as adding a new migration) - if things start to perform well, you know that your migrations are the problem. I highly recommend understanding what makes up the size here, and taking steps to no longer have huge migrations.
Beyond that, there's unfortunately little I can do to help without a repro.
OK.
First, I'm not sure 800MB for actual code is a very large number; I'm not sure what you mean by "front-end libraries" inside wwwroot, and whether these are part of what's compiled; either they are (and they matter), or they're not (in which case they're not code-related).
In any case, even 137MB for migrations is a huge amount of code, which will cause your project to suffer from potentially severe performance issues. I'm not sure how you've managed to get to 137MB - using HasData to seed large data is a frequent culprit (see the docs on this). To find out whether the migration size is indeed the problem, you can temporarily delete that folder, and then perform several operations (such as adding a new migration) - if things start to perform well, you know that your migrations are the problem. I highly recommend understanding what makes up the size here, and taking steps to no longer have huge migrations.
Beyond that, there's unfortunately little I can do to help without a repro.
Regarding the Migrations folder, I have around 248 files. Each migration includes a corresponding .Designer file, and these are quite large; most are over 1 MB each. These files are automatically generated alongside every migration. Here’s a screenshot of the Migrations folder for reference:
What's in each migration that makes it over 1MB? Are you using HasData() as I suggested above?
What's in each migration that makes it over 1MB? Are you using HasData() as I suggested above?
I don’t believe we’re using the HasData method. To be sure, I performed a solution-wide search for the keyword HasData and found no occurrences.
@RezaRahmatzadeh have you tried temporarily removing all migrations and generating a new one as I suggested above?
@RezaRahmatzadeh have you tried temporarily removing all migrations and generating a new one as I suggested above?
Yes, I removed all migration files except the ModelSnapshot file. However, when I create a new empty migration, the process still freezes, and new migration's .Designer file is generated with a size of around 1.2 MB. It seems that the snapshot content is being duplicated into the .Designer file alongside each migration.
Even when I removed the snapshot file and create a new empty migration it's generating a new model snapshot with this size 1.2 MB.
The 1.2MB size of the snapshot is something you'll have to figure out in your project; that can happen if EF models are huge, but you say above you only have ~50 entities with some relationships. A typical culprit is HasData() as I wrote above.
In any case, there really is not much we can do without a repro.
The 1.2MB size of the snapshot is something you'll have to figure out in your project; that can happen if EF models are huge, but you say above you only have ~50 entities with some relationships. A typical culprit is HasData() as I wrote above.
In any case, there really is not much we can do without a repro.
Okay, thanks! I'll look into what's causing the large model snapshot file size.