Using Entity Framework migration (first code) in production

I am just studying the use of EF migrations for our project and, in particular, for making circuit changes in production between releases.

I have already mentioned that there is an API using the DbMigration class to perform these migrations at DbMigration , but I cannot find concrete examples.

Ideally, I would like to get one DbMigration file for each database change and for those changes that will be applied automatically when the application starts from the current version to the latest version.

+61
entity-framework ef-code-first code-first-migrations ef-migrations
Jun 01 2018-12-12T00:
source share
5 answers

There is a Database Initializer that you can use to switch to the latest version at startup (or better, dbinitializer will use the first db access), MigrateDatabaseToLatestVersion , you use it as follows:

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

Regarding the presence of one file for transfer, if you enable automatic migration, you will find them in the Migrations folder (by default) in the root directory of your project.

Relevant information, with examples, is here: http://weblogs.asp.net/fredriknormen/archive/2012/02/15/using-entity-framework-4-3-database-migration-for-any-project.aspx

+75
Jun 01 2018-12-12T00:
source share

This also works:

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

You can also call:

 migrator.GetPendingMigrations(); 

to get a list of the migrations they need.

+48
Aug 6 2018-12-12T00:
source share

Since you did not specify which version of Visual Studio you are using, or Database, I will add an answer here to say that in VS2015 with Microsoft SQL Server it is now incredibly easy using the Publish tool.

You do not need to worry about the API you are talking about. Just do your work locally, changing your models, applying migrations, etc., and then when you want to go to the release / testing server, use the publish tool.

You can apply any migrations that you did locally to the remote server the first time you run the application.

As soon as you have all your migrations and everything that has been done locally (presumably in your Dev env), you publish (right-click the project, click "Publish ..." Check "Perform the first steps of code migration (it starts when the application starts ) "on the" Settings "tab, and then it will apply the migration the first time you access the application (so for the first time there will be a slight delay).

Publish a web interface using Web-Deploy

Manual: https://msdn.microsoft.com/en-us/library/dd465337(v=vs.110).aspx

I learned all this because I had to do it on a Windows 2012 server: http://www.sherweb.com/blog/how-to-install-webdeploy-on-windows-server-2012/

Good luck

+6
Jun 03 '16 at 0:11
source share

I would like to control which migrations are performed explicitly in the code, and after a lot of searching, I managed to develop the following method without the need to include the DbConfiguration class or automatic migrations:

 public static void RunMigration(this DbContext context, DbMigration migration) { var prop = migration.GetType().GetProperty("Operations", BindingFlags.NonPublic | BindingFlags.Instance); if (prop != null) { IEnumerable<MigrationOperation> operations = prop.GetValue(migration) as IEnumerable<MigrationOperation>; var generator = new SqlServerMigrationSqlGenerator(); var statements = generator.Generate(operations, "2008"); foreach (MigrationStatement item in statements) context.Database.ExecuteSqlCommand(item.Sql); } } 

And if we had such a migration:

 public class CreateIndexOnContactCodeMigration : DbMigration { public override void Up() { this.CreateIndex("Contacts", "Code"); } public override void Down() { base.Down(); this.DropIndex("Contacts", "Code"); } } 

We will use it like this:

 using (var dbCrm = new CrmDbContext(connectionString)) { var migration = new CreateIndexOnContactCodeMigration(); migration.Up(); dbCrm.RunMigration(migration); } 

Sincerely.

+2
Jan 13 '16 at 13:02
source share

To add to all the answers are already posted. The Entity Framework uses the table: dbo .__ MigrationHistory to track all migrations that are already applied to the database, to avoid starting a migration, which, for example: inserts data or changes the database schema.

If you want to run a script, for example, start adding data or changing the database schema, you can create an empty migration using the package manager console and run the script using the new migration added. Make sure you use the initializer so that EF does not flush or recreate the database every time it starts.

  public override void Up() { string directoryToSearchScripts = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\")); string scriptFilePath = Directory.GetFiles(directoryToSearchScripts, "dummy-script.sql", SearchOption.AllDirectories).FirstOrDefault(); if (!string.IsNullOrEmpty(scriptFilePath)) { string fundsSqlScript = File.ReadAllText(scriptFilePath); Sql(fundsSqlScript); } } public override void Down() { } 

When you publish the application and check the option โ€œPerform first code migrationsโ€, EF will start migrations that have not yet been applied to the database.

0
Jun 20 '19 at 9:08
source share



All Articles