Entity Framework Core:如果我们永远不会恢复迁移,删除 Migration.Designer.cs 是否安全? [英] Entity Framework Core: Is it safe to delete Migration.Designer.cs if we will never Revert a migration?

查看:12
本文介绍了Entity Framework Core:如果我们永远不会恢复迁移,删除 Migration.Designer.cs 是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个包含约 200 个表的数据库架构.为每次迁移创建的模型快照 (Migration.Designer.cs) 约为 20K 行.因此,有相当多的迁移确实会减慢我们在 CI 上的构建速度(大约 30 次迁移构建一个解决方案需要 6 分钟的迁移或 4 分钟的迁移).

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).

那么,问题是:删除旧迁移的模型快照(我们知道我们永远不会恢复)是否安全?模型快照是否用于除 Revert-Migration 之外的任何其他用途?

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?

推荐答案

模型快照是否用于除 Revert-Migration 之外的任何其他用途?

Are model snapshots used for anything else except Revert-Migration?

是的.有一些边缘情况需要它.在 SQL Server 上,这些情况是:

Yes. There are a few edge cases where it's needed. On SQL Server, those cases are:

  • 当列变窄或计算表达式改变需要重建索引时,AlterColumn
  • 当索引唯一并引用可空列时,在内存优化表上创建索引

所以大多数情况下,删除可能是安全的,但请测试您的迁移是否在删除后仍然有效.

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

.Designer.cs 文件包含一个部分类,具有 2 个属性:

The .Designer.cs file contains a partial class, with 2 attributes:

[DbContext...
[Migration...

不要忘记将这些属性复制到包含迁移代码的类(同一分部类的 Up 和 Down 方法).EF 使用这些属性来确定程序集中的迁移.

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.

从我们的项目中删除 .Designer.cs 文件后,dbContext.Database.GetPendingMigrations().Count() 返回 0.

After removing the .Designer.cs files from our project, dbContext.Database.GetPendingMigrations().Count() returned 0.

我们通过添加这些属性解决了这个问题.

We solved this problem by adding these attributes.

这篇关于Entity Framework Core:如果我们永远不会恢复迁移,删除 Migration.Designer.cs 是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆