how to create and alter productive DBs on client´s machines? RRS feed

  • Question

  • I have a question concerning EF workflow:

    I am writing a desktop application using EF 6.0.1 and Code-First. The software shall be handed out to clients for use on single PCs or in private networks which I won´t be able to "reach". As a DB I use Microsoft Serve Compact Edition 4.0. In my IDE everything works fine, I started with using a CreateDatabaseIfNotExists-Initializer and now switched to MIgarteDatabaseToLatestVersion for final testing.

    Now I wonder (should probabaly have started earlier...) how to deploy the DB on the client´s machine(s). Problem is, the DB needs to be created when starting the app for the first time, but also should migrations shall be possible, for with further versions of the software, model changes are likely to happen, so the client´s databases need to be altered without losing actual content.

    My approach was to use the database initializers mentioned above depending on wether the database file already exists or not. But that doesn´t work as planned. If I have migrations enabled in my VS project the CreateDatabaseIfNotExists-Initializer won´t work and if I use MigrateDatabaseToLatestVersion I can´t find a way to create an initial database if none exists.

    My next approach would be not to use migrations on the client´s machines but only in my developement enviroment and in case of model changes I could create SQL-scripts for new migrations and then execute them on client´s machines with each software update.

    Is that the "official" way or am I missing something?

    • Edited by tafkab76 Tuesday, February 25, 2014 8:41 AM
    Tuesday, February 25, 2014 8:38 AM


  • Just forget everything I wrote above, everything works now! I was using the wrong initializer for one of the contexts. 
    • Marked as answer by Fred Bao Thursday, March 6, 2014 7:36 AM
    Thursday, February 27, 2014 9:49 AM

All replies

  • Hello,

    >>Is that the "official" way or am I missing something?

    It seems that you want to use the automatical migrations. After you use MigrateDatabaseToLatestVersion, do you set the AutomaticMigrationsEnabled to be true in the configuration class as below?

    public class Configuration : DbMigrationsConfiguration<S28Context>
            public Configuration()
                this.AutomaticMigrationsEnabled = true;

    It will create the database by your code model if the database does not exist and if the database already exists and you do some changes on the code model e.g. to add or remove a property, it will apply these changes to database automatically.

    Usually, we will apply the migrations on an existing database and use the PCM command to do migration operations like here.

    If I misunderstand, please let me know.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Edited by Fred Bao Wednesday, February 26, 2014 3:00 AM
    Wednesday, February 26, 2014 2:57 AM
  • Hi Fred,

    thanks for answering. I think my problem is (and I probably should have mentioned that before...) that I use two different contexts on one DB. I managed to get them working at all by enabling migrations via the PMC for one context, then renaming the corresponding Configuration-class and then enabling migrations for the second context. In both Configurations I enable Automatic Migrations as you suggest.

    Now when I run the application without an existing database the DB gets created but only with the tables and the entry in the __MigrationHistory table for one context. Then I get an InvalidOperationException "Migrations is enabled for context 'PlanverwaltungModelContext' but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the 'Update-Database' command from the Package Manager Console."

    When I do so (runnning update-database for the suggested context/configuration) everything works fine, but how would I update the database on a clients machine without being able to start the app?

    Is there a solution to this probelm? Alternatively I could merge the two contexts to one, I guess.

    • Edited by tafkab76 Thursday, February 27, 2014 8:34 AM
    Thursday, February 27, 2014 8:33 AM
  • Just forget everything I wrote above, everything works now! I was using the wrong initializer for one of the contexts. 
    • Marked as answer by Fred Bao Thursday, March 6, 2014 7:36 AM
    Thursday, February 27, 2014 9:49 AM