Entity Framework Core: Is it safe to delete Migration.Designer.cs if we will never Revert a migration?

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:

  • AlterColumn when the column is narrowed or the computed expression is changed and the indexes need to be rebuilt
  • CreateIndex on a memory-optimized table when the index is unique and references nullable columns

So most of the time it's probably safe to delete, but please test that your migrations still work after doing so.


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.


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)</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)</CurrentMonth>
  <YearOfCurrentMonth>$([System.DateTime]::Now.Year)</YearOfCurrentMonth>
  <LastMonth>$([System.DateTime]::Now.AddMonths(-1).Month)</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.