Entity Framework 6.1.3 initialize existing, but empty database from code-first model

c# code-first ef-migrations entity-framework entity-framework-6

Question

While creating an application for the release on production systems, I'm running into several issues.

The circumstance:

  • Code-First Model in Entity Framework 6.1.3 with a number of migrations
  • a dedicated serviceuser for a Windows service (not Local System)
  • It is possible to put a connection string referring to the appropriate MS-SQL database in the settings (managed by a WPF-App).
  • The installation admin should set up the database to provide the serviceuser access as the dbowner, dbwriter, and dbreader. (Should suffice for next migrations)
  • The programme User shouldn't have access to the master database on SQL Servers. Because of this, the administrator must establish the appropriate DB for the Windows service before the service is launched.

I'm attempting to:

  • creating an IDatabaseInitializerMyDbContext> that, upon detection, initialises the already-existing, empty database AND updates the database in the event of an out-of-date model

What I did try:

  • Using the initializer MigrateDatabaseToLatestVersion => This one fails because the prepared but empty database lacks a __MigrationHistory table.

  • Using the initializer CreateDatabaseIfNotExists => This one doesn't work since the database is already there, thus the initializer does nothing.

  • I created my own IDbInitializer using the InitializeDatabase-method shown below:

    public void InitializeDatabase(RapasiDbContext context)
    {
        if (!context.Database.Exists()) //Shouldn't happen
        {                
          ... // Throw Execption: No prepared DB found
        }           
        try //Initialize the existing database, but only if not already done (force:false)
        {
            context.Database.InitializeDatabase(false);
        }
        catch (Exception ex)
        {
            Log.Fatal("Error while initializing the database", ex);
        }           
        try //Update the databse, if model is outdated
        {
            var dbMigrationConfig = new Configuration();
            if (dbMigrationConfig.AutomaticMigrationsEnabled && !context.Database.CompatibleWithModel(true))
            {
                Log.Info("database schame outdated, starting migration!");
                try
                {
                    var migrator = new DbMigrator(dbMigrationConfig);
                    migrator.Update();
                }
                catch (Exception ex)
                {
                    Log.Fatal("Error while migrating the database", ex);
                }
            }
        }
        catch (Exception ex)
        {
            Log.Fatal("No Migration-Histroy found!", ex);
        }
    }
    

the issue (s)

  • The Initializer is not called at all in the current attempt I'm making.
  • The use of my own InitializeDatabase function recursively results from switching to InitializeDatabase(force:true): (

The query is:

How can the InitialCommit-Up() script from my migrations be used to start an existing database?

Or am I missing a pretty clear reason why this form of DB startup is improper? I simply don't know whether there are any best practises for my circumstance.

1
2
3/28/2017 4:18:52 PM

Accepted Answer

MigrateDatabaseToLatestVersion If there is no initializer, it should still function.__MigrationHistory table. When the migrations are performed for the first time, it is intended to be produced.

You need to supply the appropriate values for the function Object() { [native code] } arguments of the original query, as you mention in your remark.MigrateDatabaseToLatestVersion while diallingSetInitializer method:

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

According to the bibliography, the constructor's boolean argument determines whether to conduct the migration using the connection information from the context that initiated initialization. If this option is absent, the context generated using the default function Object() { [native code] } or registered factory will be used to acquire the connection information (this may not be what you need).

Additionally, I believe you could be misinterpreting the AutomaticMigrations option. If you didn't explicitly execute Add-Migration after your model changed, it indicates that your migrations will be GENERATED automatically rather than APPLIED automatically. Even if AutomaticMigrations is deactivated, I assume you want your pending migrations to be carried out when your database is out of date.

1
3/31/2017 10:44:07 PM


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