MigrateDatabaseToLatestVersion and dbo.__MigrationHistory
ef-code-first, entity-framework, entity-framework-migrations
Solution
There is also another NASTY gotcha here for anyone looking for answers. Check the content of the
[dbo].[__MigrationHistory] table column ContextKey
This table Tells how the DB was created (migration mode, Create mode etc...) was created. AND with what Migration Configuration Program. I got hurt with a change of MigrationsConfiguration Class name.
Changing the entry in table worked :-) Or renaming your code back.
Problem
I am trying to deploy an MVC4 application that is built using EF5 codefist and migrations. I want the app to update the database when I in the future deploy new versions of the app with new migrations, so in Global.asax I do this: ``` Database.SetInitializer(new MigrateDatabaseToLatestVersion<GoDealMvc4Context, Configuration>()); using (var ctx = new GoDealMvc4Context()) { ctx.Database.Initialize(false); } ``` The initial database on the server is deployed by attaching an MDF file copied from my dev machine. This database contains the __MigrationsHistory system table. So this database should not need to execute any migrations, because it is up to date with latest migration. When I try to start the app on the server, it I get this error: ``` There is already an object named 'UserProfile' in the database. [SqlException (0x80131904): There is already an object named 'UserProfile' in the database.] System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +388 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +688 System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4403 System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout) +2755286 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +527 System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +290 System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement) +247 System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements) +202 System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration) +472 System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) +175 System.Data.Entity.MigrateDatabaseToLatestVersion`2.InitializeDatabase(TContext context) +150 System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +66 System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +225 System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) +208 System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) +235 GoDeal.Mvc4.MvcApplication.Application_Start() +342 ``` So apparently the app thinks it needs to apply a migration even though the __MigrationHistory table is present in the database with this content: ``` MigrationId Model ProductVersion 201210161046508_initial 0x1F8... 5.0.0.net45 ``` and the app contains a single migration class: ``` 201210161046508_initial.cs: public partial class initial : DbMigration { public override void Up() { CreateTable( "dbo.UserProfile", .... ``` So my questions are: 1) why do my application think it needs to apply this migration when the contents of the __MigrationsHistory table is as described. 2) is this the recommended way of making an application that automatically applies new migrations wehn restarted on a new version.