Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 4.3 Migrations move existing data

I'm using EF Code First 4.3 Migrations to update my database scheme. Now I have the following situation: table A needs to be removed, table B must be created and the data of table A must be copied (along with some other data) to table B. I do not have access to the DbContext in the DbMigration class, my question is how to implement this?

like image 744
Marthijn Avatar asked Jun 13 '12 09:06

Marthijn


People also ask

How do I update my database in migration?

After creating a migration file using the add-migration command, you have to update the database. Execute the Update-Database command to create or modify a database schema. Use the –verbose option to view the SQL statements being applied to the target database.

How do I run an existing migration in Entity Framework?

Run the Add-Migration InitialCreate command in Package Manager Console. This creates a migration to create the existing schema. Comment out all code in the Up method of the newly created migration. This will allow us to 'apply' the migration to the local database without trying to recreate all the tables etc.

What is automatic migration in Entity Framework?

Automatic Migrations allows you to use Code First Migrations without having a code file in your project for each change you make. Not all changes can be applied automatically - for example column renames require the use of a code-based migration.


1 Answers

In migration Up method of your migration you can use Sql method to define any SQL you need so if you use explicit migration you can put data migration code between creating Table B and removing Table A.

like image 132
Ladislav Mrnka Avatar answered Jan 04 '23 17:01

Ladislav Mrnka