Update multiple databases using code first without using console

Asked

Viewed 867 times

2

Good afternoon, you guys.

I have an application that each client has their database and I am using the code first. To update I am always running in hand the command update-database -force in the package console for each customer 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 error occurs while running update command.

I wish to have some script to update all databases automatically, ie a have a context that has more than one database on web.config.

The error that occurred is as follows:

Automatic Migration was not Applied because it would result in data Oss. Set Automaticmigrationdatalossallowed to 'true' on your Dbmigrationsconfiguration to allow application of Automatic Migrations Even if they Might cause data Oss. Alternately, use Update-Database with the '-Force' option, or scaffold an Explicit Migration.

  • Which error occurred?

  • Follow the bug @Victorstafusa 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 Oss. Alternately, use Update-Database with the '-Force' option, or scaffold an Explicit Migration.]

  • Automatic Migration was not Applied because it would result in data Oss. - Are you sure the size of the varchar fields is correct and there is no column or table missing?

  • update-database -force on console works, so that’s not the problem.

  • And without the -force?

  • It also works.

  • 1

    Well, the error message is saying there’s somewhere where it doesn’t work without the -force, And that’s why he doesn’t. And the reason he doesn’t is because some data would end up being lost somewhere. So I suggest you try to figure out which records and columns could cause some data loss in the migration and why. The -force serves to it to go over all this and not to care if in fact some data loss will occur (which can be something dangerous).

  • and how would I find that out? Do you have any idea? Using Automaticmigrationdatalossallowed = true works, plus I was worried about this data loss issue

  • 1

    No, I don’t know. But with that, I think your question has been clarified. I’m going to vote to reopen.

  • @Victorstafusa I’d better ask a new question?

  • No, don’t ask a new question. It would end up being closed as a duplicate of this. All you have to do is get four more people to reopen, which isn’t too hard, and I’m gonna help you with that.

Show 6 more comments

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 publishing your project, the migrator automatically executes when accessing the database for the first time after publishing.


EDIT

This message:

Automatic Migration was not Applied because it would result in data Oss.

It is an Entity Framework protection mechanism for database changes that will be destructive, i.e., that will result in loss of data from tables involved.

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

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

Now, if data integrity is not an issue, you can set, at Migrations/Configuration.cs, the following construction:

    public Configuration()
    {
        AutomaticMigrationDataLossAllowed = true;
    }

This will allow your Migrations destructive changes in the Schema. I don’t recommend this type of configuration for your system’s production environment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.