Sha*_*dix 7 c# ef-migrations entity-framework-core
我们有一个包含~200个表的数据库模式.为每次迁移创建的模型快照(Migration.Designer.cs)是~20K行.因此,进行大量迁移确实会降低我们对CI的构建速度(构建解决方案的约30次迁移需要6分钟进行迁移,或者4分钟不进行迁移).
那么,问题是:删除旧迁移的模型快照是否安全(我们知道我们永远不会恢复)?模型快照是否用于除Revert-Migration之外的任何其他内容?
在我当前的项目中遇到了同样的问题。.Designer 中超过 400 个迁移和 600 万行代码。以下是我设法解决此问题的方法:
迁移项目.csproj
<PropertyGroup>
...
<DefaultItemExcludes Condition="'$(Configuration)' == 'Debug' ">$(DefaultItemExcludes);Migrations\**\*.Designer.cs</DefaultItemExcludes>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
这样您就不需要重置迁移,也不需要删除 .Designer 文件。
编辑:这是一种临时解决方法,有一天您需要重置您的迁移。
除了恢复迁移以外,模型快照是否还用于其他任何用途?
是。在一些需要的情况下。在SQL Server上,这些情况是:
因此,在大多数情况下,删除它可能是安全的,但是请在此之后测试您的迁移是否仍然有效。
这是 Jaime Yule 方法的改进。
在开发中,我希望能够测试当前的迁移,并在合并其他分支时执行落在我的分支上的迁移。因此,我没有排除所有设计器文件,而是保留最新的文件,如下所示:
<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>
Run Code Online (Sandbox Code Playgroud)
当然,为了完全万无一失,还必须包括前一个月。像这样:
<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>
Run Code Online (Sandbox Code Playgroud)
最后但并非最不重要的一点是,我们决定忽略该'$(Configuration)'=='DEBUG'条件,因为我们仅在生产中滚动,而对于开发,我们使用EnsureCreated。所以没有必要保留所有迁移的历史。