Update existing database with Entity Framework Code First in MVC

前端 未结 2 1758
暗喜
暗喜 2020-12-09 12:23

In my MVC application I used Entity Framework 6 and created database with code first approach. After a certain time, I updated one of the entity classes by adding new column

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-09 12:44

    I've had this exact problem. Seems It is worth noting that there are commands in place to aid in this situation, namely the -IgnoreChanges and -Force flags.

    I was trimming from a multi-dbContext to single dbContext app. As you can guess the tables already existed, but the single context new nothing of the tables that were maintained in the second context.

    It is actually quite simple (albeit 2 days of searching for the answer to no avail led me to read up on the command lines of EF Code First Migrations and the package manager...) Here is how I handled it.

    You could delete migrations folder and _Migrations Table in SQL… this would then cause you to need to use the following: Enable-Migrations -Force

    But you should be able to pick up from here without taking drastic measures:

    1. Add-migration “Reset” –IgnoreChanges –Force (Forcibly ignores changes that may exists in your model/class – good for getting started with existing database)
    2. Update-Database (just writes migration line as a basis)
    3. Add-migration “AddMyMigrationsToThisDb” –Force (forcibly iterates object model in code to pick-up changes)
    4. Update-Database

    Now you should be back on track for just using Add-Migration and Update-Database without the extra flags.

提交回复
热议问题