Entity Framework Code First Migration

China☆狼群 提交于 2020-05-29 07:46:06

问题


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.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/35872522/entity-framework-code-first-migration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!