Using Entity Framework (code first) migrations in production

There is a Database Initializer you can use to achieve the migration to latest version on startup (or better, the dbinitializer will kick in on first db access), the MigrateDatabaseToLatestVersion, you use it like that: Database.SetInitializer<ObjectContext>( new MigrateDatabaseToLatestVersion<ObjectContext, Configuration>()); Regarding having one file per migration, if you enable automatic migrations you will find them in … Read more

How to delete and recreate from scratch an existing EF Code First database

If I’m understanding it right… If you want to start clean: 1) Manually delete your DB – wherever it is (I’m assuming you have your connection sorted), or empty it, but easier/safer is to delete it all together – as there is system __MigrationHistory table – you need that removed too. 2) Remove all migration … Read more

EntityFramework code-first custom connection string and migrations

If your migration does not work correctly try to set Database.Initialize(true) in DbContext ctor. public CustomContext(DbConnection connection) : base(connection, true) { Database.Initialize(true); } I have similar problem with migrations. And in my solution I have to always set database initializer in ctor, like below public CustomContext(DbConnection connection) : base(connection, true) { Database.SetInitializer(new CustomInitializer()); Database.Initialize(true); } … Read more

Multiple DB Contexts in the Same DB and Application in EF 6 and Code First Migrations

Entity Framework 6 added support for multiple DbContexts by adding the -ContextTypeName and -MigrationsDirectory flags. I just ran the commands in my Package Manager Console and pasted the output below… If you have 2 DbContexts in your project and you run enable-migrations, you’ll get an error (as you probably already know): PM> enable-migrations More than … Read more

Reset Entity-Framework Migrations

You need to : Delete the state: Delete the migrations folder in your project; And Delete the __MigrationHistory table in your database (may be under system tables); Then Run the following command in the Package Manager Console: Enable-Migrations -EnableAutomaticMigrations -Force Use with or without -EnableAutomaticMigrations And finally, you can run: Add-Migration Initial