While working on a project I experienced the following error:
InvalidOperationException was unhandled by user code
An exception of type ‘System.InvalidOperationException’ occurred in EntityFramework.dll but was not handled in user codeAdditional information: The model backing the ‘AdminDataContext’ context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
First I double checked if the context really changed by executing the Update-Database command in the “Package manager console”. But it told me that the database was up-to-date.
PM> Update-Database
Specify the ‘-Verbose’ flag to view the SQL statements being applied to the target database.
No pending explicit migrations.
Running Seed method.
After a couple of hours I found the solution. I needed to add a line of code to the Global.asax file:
namespace Juwelenkistje.Admin
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
Database.SetInitializer<AdminDataContext>(null);
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
This method executes some code when the DbContext (between <>) is used to access the database. SeeMSDN for more information about how to implement this method.
Hope this helps to keep you productive.
Thanks and love you.
You are the only person who has documented this, and saved me further hours – thanks!