Model backing a DB Context has changed; Consider Code First Migrations

前端 未结 13 519
旧巷少年郎
旧巷少年郎 2020-12-02 09:54

The model backing the \'MyDbContext\' context has changed since the database was created. Consider using Code First Migrations to update the database (http://

相关标签:
13条回答
  • 2020-12-02 10:11

    To solve this error write the the following code in Application_Start() Method in Global.asax.cs file

    Database.SetInitializer<MyDbContext>(null);
    
    0 讨论(0)
  • 2020-12-02 10:12

    If you have changed the model and database with tables that already exist, and you receive the error "Model backing a DB Context has changed; Consider Code First Migrations" you should:

    • Delete the files under "Migration" folder in your project
    • Open Package Manager console and run pm>update-database -Verbose
    0 讨论(0)
  • 2020-12-02 10:20

    In my case this error was caused by the existence of the _MigrationsHistory table in the database. Deleting that table fixed the problem. Not sure how that table got into our test environment database.

    0 讨论(0)
  • 2020-12-02 10:23

    This error occurs when you have database is not in sync with your model and vice versa. To overcome this , follow the below steps -

    a) Add a migration file using add-migration <{Migration File Name}> through the nuget package manager console. This migration file will have the script to sync anything not in sync between Db and code.

    b) Update the database using update-database command. This will update the database with the latest changes in your model.

    If this does not help, try these steps after adding the line of code in the Application_Start method of Global.asax.cs file -

    Database.SetInitializer<VidlyDbContext>(new DropCreateDatabaseIfModelChanges<VidlyDbContext>());
    

    Reference - http://robertgreiner.com/2012/05/unable-to-update-database-to-match-the-current-model-pending-changes/

    0 讨论(0)
  • 2020-12-02 10:24

    Adding this as another possible solution, because this is what fixed it in our case;

    Make sure if you have multiple projects that they are using the same Entity Framework Nuget package version!.

    In our case we had one project ( call if project A ) holding the EF code first context with all entities. It was this project that we were using to add migrations & update the database. However a second project ( B ) was referencing project A to make use of the context. When running this project we got the same error;

    The model backing the 'MyDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

    0 讨论(0)
  • 2020-12-02 10:24

    Entity Framework detects something about the model has changed, you need to do something to the database to get this work. Solution: 1. enable-migrations 2. update-database

    0 讨论(0)
提交回复
热议问题