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.
推荐答案
迁移有 Up 和 Down.您始终可以通过拆除迁移然后添加新迁移来重新构建您的应用程序.Down 过程不会更改您的模型,只会更改数据库.使用 Update-Database -Target:migrationTargetName 或 Update-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.
这篇关于在实体框架中合并迁移条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在实体框架中合并迁移条目
基础教程推荐
- 将数据集转换为列表 2022-01-01
- 如果条件可以为空 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
