1

We have some existing applications where we are retrofitting entity framework as an ORM.

The aim is to use code first (from existing database) with migrations, but we are having problems getting migrations to fit to the existing database scenario:

  • a test/prod database which already has all tables that matches the code. No migrationsTable though.
  • A localhost development database which is empty.

The goals

  1. a developer can run a unit test project and the database will create itself along with all tables.
  2. it will be possible to generate a script from migrations that will take care of generating the __migrationsHistory table and all changes onwards needed for production.

Using package-manager it is possible to solve 1 with default migration initializer. Or solve 2 with the ignore-changes switch. But no success having both to work simultaneously.

I have tried:

  • Add-migration InitialCreate; add-migration MigrationHistoryTable -ignore-changes;
    The idea being that developers could do: update-database
    When going into test / prod a script could be generated using update-database -Script -SourceMigration: $InitialDatabase. The problem is that the script will find the initialCreate migration to missing and attempt to re-add existing tables. A source migration from MigrationHistoryTable doesn't add the Migration history table if missing (which it is in test/prod).
  • Another attempt was to have two migration projects, one for unit test and the other database. This became a bit messy though.

Any other suggestion?

Thx Kim

1 Answer 1

1

You can use the entity framework power tools extensions to reverse engineer you existing database into code first. On a development machine with no database do an

add-migration Initial

and

update-database

and you will have model matching production. Script out the __MigrationHistory table and all of the rows and add them to your production database. Your production database will then be ready for future scripts generated from migrations.

The command

 update-database -script -SourceMigration $InitialDatabase

will generate a full script for updating your database to the latest version. It checks for the __MigrationHistory table and goes row by row apply schema changes to make the model match the changes in each migration.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Miniver, I'm searching for a solution where EF can assist in this situation without manual scripting (or autogenerated from DB). If there is no better suggestion I will mark this as being the solution. Thx Kim
Entity Frameworks migration functionality is all tied to that __MigrationHistory table. Another way to go might be to reverse engineer the database and use Entity Framework Migrations locally and then use the Schema Compare tool in Sql Server Data Tools or if I'm not understanding once the __MigrationHistory table is in production you could just change your connection string to point to production and do an update-database.
Hi Miniver, it seems like your first proposed solution is the one we will go with. Thx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.