如何强制Entity Framework将特定的迁移标记为已应用? [英] How do I force Entity Framework to mark a particular migration as having been applied?

查看:71
本文介绍了如何强制Entity Framework将特定的迁移标记为已应用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目中使用Entity Framework 5,并且启用了迁移功能。

I'm using Entity Framework 5 in a project, and I've got Migrations enabled.

在这种情况下:

一个新的开发人员(dev1)出现并从源代码构建了项目。自从以前的开发人员过去从事该项目以来,就已经存在迁移。

A new developer (dev1) comes on and builds the project from source. There are existing migrations since previous developers had been working on the project in the past.

当该开发人员首次运行ASP.NET MVC项目时,数据库将建立自动,并且不会出现错误。

When that developer runs the ASP.NET MVC project for the first time, the database builds automatically, and no errors appear.

但是,此后,另一个开发人员(dev2)添加了新的迁移。当Dev1尝试运行更新数据库时,将尝试运行所有 上一个 迁移。但是它们已经被应用,因为它们是Dev1所见的 初始模型 的一部分。

After that, however, a different developer (dev2) adds a new migration. When Dev1 tries to run Update-Database, all of the previous migrations are attempted to run. But they have already been applied, since they were part of the initial model as Dev1 saw it. This often results in a schema error since it's trying to apply a schema change that already exists.

因此,通常,这会导致模式错误,因为它试图应用已经存在的模式更改。数据库到当前的迁移。但是我不知道这样做的方法。另外,还有其他方法可以初始化数据库,以便我可以在初始化期间应用所有迁移吗?

So, optimally, it would be great to just "fast forward" the local database to the current migration. But I don't know of a way to do that. Alternatively, is there some other way to initialize the database so that I can apply all the migrations during initialization?

推荐答案

我知道了骇客。

运行 Update-Database -Script

选择排除所有已经运行的迁移

Pick out all the migrations that have already been run

INSERT INTO [__MigrationHistory] ([MigrationId], [Model], [ProductVersion]) VALUES

打开Sql Server Management Studio,并手动运行这些sql语句。

Open Sql Server Management Studio, and run those sql statements manually.

新迁移应能正常工作。

这篇关于如何强制Entity Framework将特定的迁移标记为已应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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