Kim Bendtsen
Kim Bendtsen

Reputation: 13

Migrations when using entity framework with both existing database and empty development database

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:

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:

Any other suggestion?

Thx Kim

Upvotes: 1

Views: 349

Answers (1)

Miniver Cheevy
Miniver Cheevy

Reputation: 1677

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.

Upvotes: 1

Related Questions