How to create a migration for an existing database in EntityFramework 4.3?

so I want to start with EntityFramework 4.3 migration. I wanted to know if I could convert an existing database into a database with migration support and EF to assume that only the changes from this point should be considered as migration.

+7
source share
3 answers

A nice walk for this is posted here: http://thedatafarm.com/data-access/using-ef-4-3-code-first-migrations-with-an-existing-database/

The only change I would suggest is simply commenting out the code in the Up and Down methods until you expand the migration. After that, you can uncomment the code, and this will allow you to create a new database if you need to later.

+9
source

So it seems that I was looking for Codebased Migrations , which is activated when I set AutomaticMigrationsEnabled = false. My models were created from an existing database. To activate the migration, all I had to do was enable the Enable-Migrations, create a new new migration file using Add-Migration, disable it (my models are already in the database, so I don’t want EF tried and create them) and deploy. To deploy, I added the following to the Global.asax file:

protected void Application_Start() { var config= new Configuration(); var migrator = new DbMigrator(config); migrator.Update(); } 

A new __MigrationHistory table was created and a new migration record was created in it. This new migration entry had a hash of my models, so now any changes to my models can be written for me in future migrations with EF.

To test, I created another migration file (Add-Migration), added a new property to the model, launched Add-Migrations, in which a new field was written, and then deployed my application. Migration performed as expected.

+8
source
0
source

All Articles