Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity framework code first migrations production (WPF application)

I recently wrote a WPF application with code first back-end (SQL CE 4.0 database). The application is now out in the wild and is being used by non-technical people.

I have made some changes and I need to add a migration to reflect the changes, which I have done successfully. I can get this to work fine on my local machine by simply using the Update-Database command.

How will this work when I come to deploy the update? I can't seem to figure how my customers databases will get migrated. Whenever I run on a machine that isn't my development machine, I simply get the following error;

Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.

P.s. I definitely don't want to enable Automatic Migrations ;)

Update ----

Here is my initialiser;

Database.SetInitializer(new Initializer());

Here is my actual initialiser;

public class Initializer : MigrateDatabaseToLatestVersion<Context, Configuration>
{
}

And here is the constructor for my Configuration class;

public Configuration()
{
    AutomaticMigrationsEnabled = false;

    DbMigrator migrator = new DbMigrator(this);

    if (migrator.GetPendingMigrations().Any())
    {
        _pendingMigrations = true;
        migrator.Update();
    }
}
like image 605
Jon Preece Avatar asked Oct 29 '25 09:10

Jon Preece


1 Answers

You lack the initializer:

Database.SetInitializer(
  new MigrateDatabaseToLatestVersion<YourContext, YourMigrationsConfiguration>());

This will will force your customers databases to be upgraded when your application launches.

Another option which gives you more control over the migration process is to force it manually:

    YourMigrationsConfiguration cfg = new YourMigrationsConfiguration(); 
    cfg.TargetDatabase = 
       new DbConnectionInfo( 
          theConnectionString, 
          "provider" );

    DbMigrator dbMigrator = new DbMigrator( cfg );
    dbMigrator.Update();             
like image 50
Wiktor Zychla Avatar answered Oct 31 '25 00:10

Wiktor Zychla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!