合并实体框架中的迁移条目 [英] Merging migration entries in Entity Framework

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

问题描述

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

该模型现已稳定,无需保留已经存在的迁移历史记录.

是否可以重置模型并将所有迁移命令合并到初始迁移中?

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

解决方案

迁移同时具有UpDown.您总是可以通过拆分迁移然后添加新迁移来重新对应用程序进行脚手架. Down进程不会更改您的模型,只会更改数据库.使用Update-Database -Target:migrationTargetNameUpdate-Database -TargetMigration:migrationNumber.

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

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

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?

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.

解决方案

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.

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.

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.

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

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