Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force Entity Framework to mark a particular migration as having been applied?

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

Here's the scenario:

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.

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

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?

like image 589
Doug Avatar asked Jan 11 '13 17:01

Doug


1 Answers

I figured out a hack.

Run Update-Database -Script

Pick out all the migrations that have already been run

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

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

New migrations should work fine.

like image 157
Doug Avatar answered Sep 30 '22 19:09

Doug