Update multiple database using code first without using console

2

Good afternoon, guys.

I have an application that every client owns their database and I'm using code first. To update I'm always running the command update-database -force in the package console for each client database. Does anyone know a way to automate this process? I did a search and found this link entity-framework-code-first-migration -to-multiple-database , however, an error occurred while executing the update command.

I want to have some script to update all databases automatically, that is to say I have a context that has more than one database in web.config .

The error that occurred is as follows:

  

Automatic migration was not applied because it would result in data loss. Set AutomaticMigrationDataLossAllowed to 'true' on your DbMigrationsConfiguration to allow application of automatic migrations even if they might cause data loss. Alternately, use Update-Database with the '-Force' option, or scaffold an explicit migration.

    
asked by anonymous 23.08.2016 / 21:26

1 answer

2

Set the following in your configuration file ( Web.config ):

<configuration>
  ...
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
    <contexts>
      <context type="MeuSistema.Models.MeuSistemaContext, MeuSistema">
        <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion'2[[MeuSistema.Models.MeuSistemaContext, MeuSistema], [MeuSistema.Migrations.Configuration, MeuSistema]], EntityFramework, PublicKeyToken=b77a5c561934e089">
          <parameters>
            <parameter value="DefaultConnection" />
          </parameters>
        </databaseInitializer>
      </context>
    </contexts>
  </entityFramework>
  ...
</configuration>

When you publish your project, the migrator automatically runs when you access the database for the first time after publishing.

EDIT

This message:

  

Automatic migration was not applied because it would result in data loss.

It is a protection mechanism of the Entity Framework for changes in databases that will be destructive, that is, that will result in data loss of the involved tables.

Before running any Migrations , make a backup of your database.

The correct one, when this occurs, is to restart the migration history (table __MigrationHistory ), erasing all Migrations and generating a new Migration . After that, restore the data from the backup in the database.

Now, if data integrity is not a problem, you can set, in Migrations/Configuration.cs , the following construct:

    public Configuration()
    {
        AutomaticMigrationDataLossAllowed = true;
    }

This will allow your Migrations destructive changes to Schema . I do not recommend this type of configuration for the production environment of your system.

    
23.08.2016 / 22:49