EF Code First with existing database on another database

c# ef-migrations entity-framework-6

Accepted Answer

Let's imagine you have two databases, DEV and PROD. Before migrations are enabled, both are in the same state and share the same schema. What you should do is:

1 - Set your database initializer to MigrateDatabaseToLatestVersion and add migrations to your DEV environment. The alternative is to run migrations systematically.

enable-migrations
// take a snapshot of current state. -IgnoreChanges prevents recreate of existing objects.
add-migration InitialBaseline -IgnoreChanges
update-database

2 - There are many methods for keeping the other database(s) synchronised:

A) Keep migrations running concurrently by modifying the connection string. So run and point towards PROD.update-database to apply the initial, blank baseline and construct the __MigrationHistory table. I do not advise using this option for PROD databases (see below).

B) Integrate scripts. Many firms want DBAs to apply scripts rather than EF since they don't want EF making modifications. You may want to set your database initializer to NULL for this option. If so, you may do anupdate-database -Script to bring about changes. Since they are already in sync, this would be done on migrations that come after your original baseline. For additional information on this approach, see here.

C) Use a diff tool or database projects to keep everything up to date.

As soon as you update your models in DEV:

add-migration Changes1
update-database

Change the connect string for option A, then do it again. Utilize option B.update-database -Script Use a tool to resync for option C.

IMPORTANT: "I have to turn on automatic migrations." - Automatic migrations are a whole other issue and may make the procedure more difficult. Look at here.

1
3/6/2018 6:11:46 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