Using Entity Framework (code first) migrations in production

.net ef-code-first ef-migrations entity-framework

Question

To make schema changes in production between releases, I'm specifically looking at utilizing EF migrations for our project.

I've read that there is an API to carry out these migrations utilizing the run-timeDbMigration class, but I am unable to locate any particular instances.

Ideally, I'd want one.DbMigration Every time a database is changed, a file is created, and all database modifications are automatically applied from the earliest version of the program to the most recent version.

1
65
6/1/2012 10:52:57 AM

Accepted Answer

To perform the migration to the most recent version at startup, you may utilize a Database Initializer (or, better yet, the dbinitializer will activate upon first database access).MigrateDatabaseToLatestVersion , you use it in this way:

Database.SetInitializer<ObjectContext>(
    new MigrateDatabaseToLatestVersion<ObjectContext, Configuration>());

If you activate automatic migrations, you will discover them under the "One file per migration" section of theMigrations folder (by default) at the project's root.

Here is pertinent information and examples: http://weblogs.asp.net/fredriknormen/archive/2012/02/15/using-entity-framework-4-3-database-migration-for-any-project.aspx

76
1/17/2013 1:33:39 PM

Popular Answer

This also works:

var configuration = new MyDbContextConfiguration();
configuration.TargetDatabase = new DbConnectionInfo(
    database.ConnectionString, database.ProviderName);

var migrator = new DbMigrator(configuration);
migrator.Update();

Additionally, dial:

migrator.GetPendingMigrations();

to get a list of the migrations it must implement.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow