Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Code First Migration

We are using Entity Framework code first migration technique for a while now. Everything works good however our Migrations folder has been growing big with numerous migration files because of changes we are making on entity schema. I was curious, is there a way we can update just one or specific number of files whenever there is change in entity so that our Migrations folder would look less messy. Also, i don't want to drop the table and recreate it since i will be loosing all the saved data.

like image 757
sanjeev40084 Avatar asked Sep 25 '26 22:09

sanjeev40084


2 Answers

You could simply merge all of your migrations into a new file (if you aren't concerned with keeping every migration).

To do so, simply delete every migration currently in the folder, and rerun the enable-migrations command and then the add-migration command like so.

Enable-Migrations

add-migration InitialCreate
like image 174
JD Davis Avatar answered Sep 27 '26 12:09

JD Davis


You can try to use database initializer called MigrateDatabaseToLatestVersion. That automatically updates the database schema, when your model changes without losing any existing data or other database objects.

Database.SetInitializer(
new MigrateDatabaseToLatestVersion<YourDbContext, 
DataLayer.Migrations.Configuration>());
like image 26
Onur Çelik Avatar answered Sep 27 '26 10:09

Onur Çelik