Taz Uddin
Taz Uddin

Reputation: 27

The model backing the 'ScopoContext' context has changed since the database was created. Consider using Code First Migrations to update the database

Background: Everything was going smoothly until I created a new Model class and changed one of existing Model classes. And I Migrated successfully.

The situation is I have to run add-migration and update-database every time I build my project to avoid this exception. I have no idea what's going on. Please help.

Additional: automatic-migration is set to true. I checked the _MigrationHistory table and every migration is present there

Upvotes: 2

Views: 1483

Answers (1)

Divyang Desai
Divyang Desai

Reputation: 7866

You have to add code to your DbContext class:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    Database.SetInitializer<YourDbContext>(null);
    base.OnModelCreating(modelBuilder);
} 

EDIT :
When a model is first created, we run a DatabaseInitializer to do things like create the database if it's not there or add seed data.

The default DatabaseInitializer tries to compare the database schema needed to use the model with a hash of the schema stored in an EdmMetadata table that is created with a database (when Code First is the one creating the database).

Existing databases won’t have the EdmMetadata table and so won’t have the hash and the implementation today will throw if that table is missing.

We'll work on changing this behavior before we ship the fail version since it is the default. Until then, existing databases do not generally need any database initializer so it can be turned off for your context type by calling:

Database.SetInitializer<YourDbContext>(null);

Upvotes: 5

Related Questions