本文介绍了代码优先迁移实体框架6上/下清空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是,当我添加迁移"时,似乎没有完成与目标数据库的比较,并且Up/Down方法为空.

The problem I'm having is that when I "Add-Migration", it seems that no comparison against the target database is done and the Up/Down methods are empty.

这从我针对不同的环境开始.当我更新数据库时,迁移更改工作正常,它以一个特定的数据库(开发数据库)为目标.

This starts from me targeting different environments. The migration changes work fine when I update-database and it targets one particular database, my dev database.

当我更改连接字符串以目标另一个数据库,另一个服务器上的另一个dev框为目标,并尝试为该特定环境添加迁移时,Up/Down方法始终为空.

When I change the connection string to target another database, another dev box on another server, and try to add-migration for that particular environment, the Up/Down methods are always empty.

我尝试将更新数据库定位为在第二个开发者框中回滚对先前迁移的更改,但是它没有任何改变.

I tried targeting an update-database to roll back changes to a previous migration at the 2nd dev box but it didn't change anything.

如果我将代码优先更改从一个环境迁移到另一个环境,我需要做些特别的事情吗?

Is there anything that I need to specifically do if I'm migrating code-first changes from one environment to another?

推荐答案

您不需要添加新的迁移,您需要强制EF运行所有旧迁移.尝试指定源迁移和目标迁移:

You don't need to add a new migration, you need to force EF to run all the old ones. Try specifying the source and target migrations:

Update-Database -SourceMigration: $InitialDatabase -TargetMigration: AddPostAbstract

另一种选择是生成脚本,您可以通过在命令中添加-Script来针对其他数据库运行该脚本.

Another option would be to generate a script that you can run against the other database by adding -Script to the command.

请参见 https://msdn.microsoft.com/zh-CN/data/jj591621.aspx#script

如果您有很多迁移,则可以通过删除它们并进行新的添加迁移来扮演它们的角色.

If you have a lot of migrations you could role them up by deleting them and doing a fresh add-migration.

这篇关于代码优先迁移实体框架6上/下清空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 15:02