Error when adding a new field to database context

OnlyPrograming

I am programming an MVC 4 ASP.NET application. I keep getting this error when I want to add a new field to the database:

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

What should I do?

Mihai Dinculescu

You get that error because your database is no longer in sync with your DbContext classes.

You should look into using migrations

You work with code first migrations using the Package Management Console.

enable-migrations

Does exactly what the name implies. Initializes migrations in your project. This will create a folder inside your project and generate the files needed.

add-migration InitialCreate

This creates a migration. InitialCreate is actually a string and you can change it to whatever you want. This command will generate the scripts needed to create the database from strach.

update-database

This command verifies the database and applies the migration (or migrations - there can be multiple) required in order to get the database up-to-date.

This is the initial setup. If you do further changes to your first code first classes, or add more, you will just have to add a new migration and then execute it.

add-migration AddedFirstName
update-database

It's that simple!

There are some more advanced concepts like seed, rollback, update to specific migration, etc., but what I have typed above covers the basics and the day to day usage of migrations.

I recommend you to read this article which explains everything in much more detail: http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

updating dataset when adding a new field in database in c#

From Dev

updating dataset when adding a new field in database in c#

From Dev

Backward compatibility when adding a new field in response

From Dev

Adding a media field when creating a new entry

From Dev

NullReferenceException error when executing SaveChanges to database context

From Dev

MySQL error Unknown column in field list when migrate the database to new server (Windows)

From Dev

Error when getting ContextMenu and adding new item

From Dev

Ionic Error when adding new platform

From Dev

Adding a login field to a database?

From Dev

Adding a new field to an entity

From Dev

Error adding a new field to 1 model in Django 1.7

From Dev

Error adding a new field to 1 model in Django 1.7

From Dev

Invalid context error on iOS 7 when adding a UIPickerView inside a UIActionSheet?

From Dev

select option created with var not displaying when adding a new field dynamically

From Dev

Adding a new Field to FDMemTable when loading an existing data

From Dev

Error when migrating WordPress database to new server

From Dev

Adding a new field in the DhtmlX library

From Dev

Adding a new field in SQLite in android

From Dev

"Syntax error in INSERT INTO statement" when adding record to Access database

From Dev

Incorrect String error when adding emoji to a database through a form

From Dev

Error when adding another record to dynamic memory database

From Java

Error when adding new table on service.xml - Liferay 6.2

From Dev

NullReference error when adding a new model into a model property

From Dev

Error with cookie-value when adding a new Spring Session

From Dev

Error when adding new ASP.Net Web Application

From Dev

Error with cookie-value when adding a new Spring Session

From Dev

Formula error message when adding new rows to tables in Excel

From Dev

TypeError: ... is not JSON serializable error when adding new values to an object by Python

From Dev

SSAS error message in Excel when adding new dimension

Related Related

  1. 1

    updating dataset when adding a new field in database in c#

  2. 2

    updating dataset when adding a new field in database in c#

  3. 3

    Backward compatibility when adding a new field in response

  4. 4

    Adding a media field when creating a new entry

  5. 5

    NullReferenceException error when executing SaveChanges to database context

  6. 6

    MySQL error Unknown column in field list when migrate the database to new server (Windows)

  7. 7

    Error when getting ContextMenu and adding new item

  8. 8

    Ionic Error when adding new platform

  9. 9

    Adding a login field to a database?

  10. 10

    Adding a new field to an entity

  11. 11

    Error adding a new field to 1 model in Django 1.7

  12. 12

    Error adding a new field to 1 model in Django 1.7

  13. 13

    Invalid context error on iOS 7 when adding a UIPickerView inside a UIActionSheet?

  14. 14

    select option created with var not displaying when adding a new field dynamically

  15. 15

    Adding a new Field to FDMemTable when loading an existing data

  16. 16

    Error when migrating WordPress database to new server

  17. 17

    Adding a new field in the DhtmlX library

  18. 18

    Adding a new field in SQLite in android

  19. 19

    "Syntax error in INSERT INTO statement" when adding record to Access database

  20. 20

    Incorrect String error when adding emoji to a database through a form

  21. 21

    Error when adding another record to dynamic memory database

  22. 22

    Error when adding new table on service.xml - Liferay 6.2

  23. 23

    NullReference error when adding a new model into a model property

  24. 24

    Error with cookie-value when adding a new Spring Session

  25. 25

    Error when adding new ASP.Net Web Application

  26. 26

    Error with cookie-value when adding a new Spring Session

  27. 27

    Formula error message when adding new rows to tables in Excel

  28. 28

    TypeError: ... is not JSON serializable error when adding new values to an object by Python

  29. 29

    SSAS error message in Excel when adding new dimension

HotTag

Archive