哪些迁移文件可以安全删除

发布于 2025-01-26 16:18:42 字数 286 浏览 5 评论 0原文

我正在创建一个新的代码优先网络应用程序,但使用来自另一个应用程序的代码作为起点。但是我宁愿从新项目中删除所有不需要的迁移。

目前,我的迁移看起来像这样:

所以我的问题是:我可以安全地删除所有其他迁移文件吗?

注意:没有变化,我将要恢复新应用程序中的迁移,因此这不是一个问题。

I'm creating a new code-first Web application but using the code from another application as a starting point. But I'd prefer to delete all the unneeded migrations from the new project.

Currently, my migrations look like this:

Visual Studio Data Migrations

I'm pretty sure that I need to keep ApplicationDbContextModelSnapshot.cs and ApplicationDbContext.cs.

So here are my question is: Can I safely delete all the other migration files?

Note: There is no change I will want to revert migrations in the new application, so that is not a concern.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小清晰的声音 2025-02-02 16:18:42

总结您的问题,您希望拥有一个干净的板岩,并且没有随着时间的流逝而污染迁移文件夹的所有迁移。在启动新项目并经常迭代之前,我经常需要自己执行此操作,然后才能满足我的最终模式。

好消息是,Entity Framework本身支持此框架,只有一个警告:如果您曾经旋转自己的迁移(通过调用add odd-crigration,然后编辑结果迁移文件) ,也许出于完美的原因添加自定义索引,您必须确保在此过程中恢复这些索引。

您所要做的就是删除整个迁移文件夹(包括快照),然后重新开始,好像您从头开始创建项目一样。通常,当您创建第一个迁移时,您已经定义了一些实体(这就是为什么要创建迁移的原因,毕竟);您只是将其提升到一个新的水平,并希望您的整个数据库在该迁移中。

删除迁移文件夹后,EF将不再认为您的项目完全有迁移 - 就像我说的那样,您从头开始。因此,您要做的就是调用add迁移[migrationName](即添加移民初始迁移),它将用创建整个数据库所需的一切迁移。

注释:在我的评论中,我建议您首先需要致电启用移民,但这显然不再需要,因此您可以直接跳入调用添加移民

To summarize your question, you'd like to have a clean slate and not have all the migrations you've accrued over time polluting your migrations folder. I've often needed to do this myself when starting a new project and iterating a lot before being satisfied with my final schema.

The good news is that Entity Framework supports this natively out of the box, with just one caveat: if you ever hand-roll your own migrations (by calling Add-Migration and then editing the resultant migration file), perhaps to add a custom index for perf reasons, you'll have to make sure to recover those in this process.

All you have to do is delete your entire migrations folder (including the snapshot) and start over as though you were creating the project from scratch. Typically when you create your first migration, you've already defined a few entities (that's why you want to create the migration, after all); you're just taking that to the next level and want your entire database in that migration.

Once you've deleted your migrations folder, EF no longer thinks your project has migrations at all -- like I said, you're starting from scratch. So all you have to do is call Add-Migration [MigrationName] (i.e. Add-Migration InitialMigration) and it will scaffold the migration with everything needed to create your whole database.

Note: In my comments, I suggested you would need to call Enable-Migrations first, but that is evidently no longer necessary so you can just jump right into calling Add-Migration.

小瓶盖 2025-02-02 16:18:42

为什么不删除它们并添加空迁移以进行检查呢?如果破裂,那么您的答案。然后只需从版本控制中恢复即可。

但是柯克·沃尔(Kirk Woll)说的是正确的。保持您的应用程序dbContext并添加Intial迁移。

Why not just delete them and add an empty migration to check? If it breaks then you have your answer. Then just restore from version control.

But what Kirk Woll said is correct. Keep your ApplicationDbContext and add an intial migration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文