Search code examples
asp.net-mvcasp.net-mvc-3entity-frameworkrazorasp.net-mvc-3-areas

Update model in ASP.NET MVC 3


I am very new in MVC trying to expand my asp.net knowledge after almost 6+ years working with web forms.

MVC is really abstract to me so far but what I like is the clean coding used. I am reading "Pro ASP.NET MVC 3 Framework" book and now I am stuck at one point and I don't know how to continue honestly.

Here is the error I am getting:

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

That happen after I tried updating one Product. The page posted back but nothing got updated in the DB.

Browsing the web I found this temp solution, putting this line in the global.asax file solved the problem: Database.SetInitializer<SportsStore.Domain.Concrete.EFDbContext>(null); but now blank pages are coming up instead pages filled up with data.

I would like to know how I can move forward what exactly do I need to do to fix this.


Solution

  • There are many blogs and articles online explaining migrations, I would use the following two links as reference. In short, you've changed your model, but the tables in your database haven't been updated yet. You can either delete the tables in your database, and allow EF to recreate them, undo the changes to your models, or (MVC 4 only?) run a "migration", which is more or less a fancy way of saying allow EF to create an SQL script to update your SQL tables to match the current version of your model.

    http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx

    http://coding.abel.nu/2012/03/ef-migrations-command-reference/