简体   繁体   中英

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.

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

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>());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM