在实体框架中合并迁移条目

Merging migration entries in Entity Framework(在实体框架中合并迁移条目)

本文介绍了在实体框架中合并迁移条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Entity Framework 6 CF 项目,已经进行了一些迁移.

I have an Entity Framework 6 CF project that has a few migrations already in place.

模型现在已经稳定,不需要保留已经存在的迁移历史.

The model is now stable and there is no need to keep the migration history that already exists.

有没有办法重置模型并将所有迁移命令合并到初始迁移中?

Is there a way to reset the model and merge all migration commands into the initial migration?

例如,第一次迁移添加了一个列,而第二次迁移添加了一个唯一的非聚集索引.我现在想直接在 OnModelCreating 中查看所有这些更改,而不是在单独的迁移中.

As an example, the first migration adds a column while the second migration adds a unique, non-clustered index. I now want to see all these changes directly in OnModelCreating rather than in separate migrations.

推荐答案

迁移有 UpDown.您始终可以通过拆除迁移然后添加新迁移来重新构建您的应用程序.Down 过程不会更改您的模型,只会更改数据库.使用 Update-Database -Target:migrationTargetNameUpdate-Database -TargetMigration:migrationNumber.

Migrations have both an Up and Down. You can always Re-Scaffold your application by tearing the migrations down and then adding a new migration. The Down process does not change your model, only the changes to the database. Use Update-Database -Target:migrationTargetName or Update-Database -TargetMigration:migrationNumber.

如果您想要一个从无数据库开始并以当前模型结束的迁移,您可以使用 Update-Database -TargetMigration:0 删除所有迁移.最好先关闭数据库,然后运行 ​​Update-Database 作为测试,以验证数据库更改是否同步.

If you want a migration which starts with no database and ends with your current model, you can tear all the migrations down with Update-Database -TargetMigration:0. It's a good idea to tear down the database and then run Update-Database as a test to verify the database changes are all in sync.

请记住,如果您将迁移分解到 0,然后运行 ​​Add-Migration,您将需要非常仔细地查看生成的脚手架,因为它可能与增量更改大不相同.

Bear in mind, if you tear your migrations down to 0 and then run an Add-Migration, you will want to look very closely at the generated scaffold, as it will likely be drastically different than the incremental changes.

这篇关于在实体框架中合并迁移条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在实体框架中合并迁移条目

基础教程推荐