Reputation: 6109
We have a database schema with ~200 tables. Model snapshot (Migration.Designer.cs) which is created for each migration is ~20K lines. So, having quite a number of migrations really slows down our build on CI (with ~30 migrations building a solution takes 6 minutes with migrations or 4 minutes without them).
So, to the question: is it safe to delete model snapshots for old migrations (that we know we will never Revert)? Are model snapshots used for anything else except Revert-Migration?
Upvotes: 30
Views: 17058
Reputation: 10363
This is a refinement of Jaime Yule's approach.
In development, I want to be able to test my current migration and to execute migrations that land on my branch when merging other branches. Therefore, instead of excluding all the designer files, I keep the latest ones like this:
<PropertyGroup Condition="'$(Configuration)'=='DEBUG'">
<CurrentYear>$([System.DateTime]::Now.Year)</CurrentYear>
<CurrentMonth>$([System.DateTime]::Now.Month.ToString("00"))</CurrentMonth>
<DefaultItemExcludes>$(DefaultItemExcludes);Migrations\*.Designer.cs</DefaultItemExcludes>
</PropertyGroup>
<ItemGroup>
<Compile Include="Migrations\$(CurrentYear)$(CurrentMonth)*.Designer.cs" />
</ItemGroup>
Of course to be totally bulletproof, one would have to also include the month before. Like this:
<PropertyGroup Condition="'$(Configuration)'=='DEBUG'">
<CurrentMonth>$([System.DateTime]::Now.Month.ToString("00"))</CurrentMonth>
<YearOfCurrentMonth>$([System.DateTime]::Now.Year)</YearOfCurrentMonth>
<LastMonth>$([System.DateTime]::Now.AddMonths(-1).Month.ToString("00"))</LastMonth>
<YearOfLastMonth>$([System.DateTime]::Now.AddMonths(-1).Year)</YearOfLastMonth>
<DefaultItemExcludes>$(DefaultItemExcludes);Migrations\*.Designer.cs</DefaultItemExcludes>
</PropertyGroup>
<ItemGroup>
<Compile Include="Migrations\$(YearOfCurrentMonth)$(CurrentMonth)*.Designer.cs" />
<Compile Include="Migrations\$(YearOfLastMonth)$(LastMonth)*.Designer.cs" />
</ItemGroup>
And last but not least, we have decided to omit the '$(Configuration)'=='DEBUG'
condition as we are only rolling forward in production and for development we use EnsureCreated. So there's no need to keep the history of all migrations.
Upvotes: 5
Reputation: 4394
Based on answers by Jaime Yule, Dejan and bricelam, I've created an MSBuild task that tries to solve this automatically. All you have to do is install the task.
It automatically moves these attributes from the *.Designer.cs
file to the main code file:
[DbContext...
[Migration...
It sets the *.Designer.cs
files to not compile:
<DefaultItemExcludes>$(DefaultItemExcludes);**\*.Designer.cs</DefaultItemExcludes>
It then looks for the latest N
(configurable count) of *.Designer.cs files and specifically enables them for compilation by
<Compile Include="xxx" />
Install:
dotnet add package MSBuild.EntityFrameworkCore.RemoveDesignerCompilation --prerelease
(You have to use --prerelease
until there is a stable version published. I'd like to gather some pre-release feedback first)
Upvotes: 3
Reputation: 30415
Are model snapshots used for anything else except Revert-Migration?
Yes. There are a few edge cases where it's needed. On SQL Server, those cases are:
So most of the time it's probably safe to delete, but please test that your migrations still work after doing so.
The .Designer.cs file contains a partial class, with 2 attributes:
[DbContext...
[Migration...
Don't forget to copy these attributes to the class containing your migration code (the Up and Down methods of the same partial class). EF uses these attributes to determine which migrations are in the assembly.
After removing the .Designer.cs files from our project, dbContext.Database.GetPendingMigrations().Count() returned 0.
We solved this problem by adding these attributes.
Upvotes: 24
Reputation: 61
The .Designer.cs file contains a partial class, with 2 attributes:
[DbContext...
[Migration...
Don't forget to copy these attributes to the class containing your migration code (the Up and Down methods of the same partial class). EF uses these attributes to determine which migrations are in the assembly.
After removing the .Designer.cs files from our project, dbContext.Database.GetPendingMigrations().Count() returned 0.
We solved this problem by adding these attributes.
Upvotes: 5
Reputation: 1081
Got the same problem on my current project. Above 400 migraitons and 6m lines of code inside .Designer. Here is how I managed to resolve this problem:
MigrationProject.csproj
<PropertyGroup>
...
<DefaultItemExcludes Condition="'$(Configuration)' == 'Debug' ">$(DefaultItemExcludes);Migrations\**\*.Designer.cs</DefaultItemExcludes>
</PropertyGroup>
This way you dont need to reset migration neither delete .Designer files.
Edit: This is a temporarily workaround, you will need to reset your migraitons one day.
Upvotes: 11