0

I just deployed a web application from local host with SQL express (not IIS local host) to production.

The web application has controllers, models, views (MVC). I made some modifications to the database tables. Each time I added a new field I:

enable-migrations
add-migration addnewfieldname
update-database -verbose

All the models get updated with the new table field. The above worked well with my web application on the local host with SQL express.

So then I published it to production with all the migrations. also on production I alter the tables on the database in SQL server to have all the newly created fields.

Now comes the problem, production is not working. what did I do wrong? It gives me error page.

Here is error message:

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

Update

So I found the solution, this is amazing!

First, make sure there are no changes to the UserProfile class. Make sure you are connected to the production db first in your web.config then run:

Enable-Migrations
Add-Migration InitialMigrations -IgnoreChanges
**this below here is key, you need to add the new field one at a time.**
add-migration addnewfield1
update-database

add-migration addnewfield2
update-database


add-migration addnewfield3
update-database

etc.

This should generate a blank "InitialMigration" file. Now, add any desired changes to the UserProfile class. Once changes are added, run the update command again:

update-database -verbose

Now the automatic migration will be applied and the table will be altered with your changes.

Another key, if you don't have permissions on SQL Server to alter tables, make sure you send the scripts to your sysadmin as it won't alter the tables. This is after you have done all the above.

halfer
  • 19,824
  • 17
  • 99
  • 186
NULL
  • 1,559
  • 5
  • 34
  • 60
  • not working is not that clear, and ARGH is a kind of Exception I never heard about ;) – Raphaël Althaus Aug 23 '13 at 13:02
  • hi Raphael i am pulling my hair (hence argh:) so what did i do wrong? any ideas Raphael? – NULL Aug 23 '13 at 13:03
  • Well what I'm asking you is a little bit more informations. With just "production is not working" we can't help you... So the site is not starting, you've got exceptions, you don't have data, what ? – Raphaël Althaus Aug 23 '13 at 13:04
  • site is not working that is whats happening. it gives me an error page. the site worked on localhost, but its not working on production. clearly it is not syncing in with the database changes. – NULL Aug 23 '13 at 13:05
  • What error message is displayed on the error page? If there's no error message, go into your web.config and under the section, add (or change that element if it already exists). This will allow you to see what the actual error message is. just make sure you change/remove that setting when you've resolved this as you don't want your end users seeing a full stack trace when there's an error. – chris.house.00 Aug 23 '13 at 13:07
  • Did you update tables on production manually to correspond to your model? That's what I understand from your post. – VsMaX Aug 23 '13 at 13:09
  • @Michael yes, i "alter table mytable add newfield nchar..." here is the error: – NULL Aug 23 '13 at 13:10
  • So you probably have model in your database that doesnt correspond to your model, however I cannot tell more without any error code. – VsMaX Aug 23 '13 at 13:14
  • The model backing the 'DefaultConnection' context has changed since the database was created. Consider using Code First Migrations to update the database – NULL Aug 23 '13 at 13:14
  • this is the [solution][1] from this link, it WORKED like a CHARM, thank God!!!!! [1]: http://stackoverflow.com/questions/13238203/automatic-migrations-for-asp-net-simplemembershipprovider – NULL Aug 23 '13 at 14:45

2 Answers2

0

The error means that when you updated database manually, you made some mistakes, so the model in your database does not reflect your entites.

  1. I used to change connection string in my web.config file to to my production database and run "Update-Database", if database you are working on is already populated with some data that you don't want to lose then better just copy tables (including Migrations table) from your local sql.

  2. It's bad idea to update tables on production manually, because you might make many mistakes and EF won't work then.

Best way to fix this is to delete all tables from database (if you can afford it), change your connection string from local to production (in web.config) and run "Update-Database" command from Visual Studio.

If you cannot drop database because it's already populated with some data, copy manually all tables from local that Entity Framework has created including migrations table, then populate migrations table with data from local. That might help but I can't guarantee it.

VsMaX
  • 1,685
  • 2
  • 16
  • 28
0

It looks like your schema of db have been changed.

Had the same issue. But I have the production server which have to work 24/7. For situation like that I use:

http://www.devart.com/dbforge/sql/datacompare/

this will help you to compare the data in your local db and production server db. But if you need to compare the schema use this one:

http://www.devart.com/dbforge/sql/schemacompare/

Maris
  • 4,608
  • 6
  • 39
  • 68
  • i found the solution here: http://stackoverflow.com/questions/13238203/automatic-migrations-for-asp-net-simplemembershipprovider – NULL Aug 23 '13 at 14:47