Last post May 06, 2015 02:08 PM by Siva Krishna Macha
May 06, 2015 10:57 AM|bobh0526|LINK
This is probably an easy one but I'm new to MVC and trying to learn. I created a model named Employee and then created a controller (EmployeeController) based on this model. A view was created and I added the Employee view to my menu. I could click on
menu option Employee and I got a CRUD grid. Everything worked fine. But then I went into my Employee model and added some attributes like [Required] and [Display]. Now when I run my MVC 5 project I get this error:
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
Additional information: The model backing the 'MyDBContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
So how do I apply my changes so that I don't get this error?
May 06, 2015 12:31 PM|Siva Krishna Macha|LINK
I believe, you would need to enable the Code-First migration and then Add migration to get the database updated.
The help link in your error message is the way to go to proceed further.
To open Package Manager Console: Tools –> Library Package Manager –> Package Manager Console
1. Run the Enable-Migrations command in Package Manager Console.
2. Add-Migration will scaffold the next migration based on changes you have made to your model since the last migration was created
3. Update-Database will apply any pending migrations to the database
May 06, 2015 01:48 PM|bobh0526|LINK
Thanks Siva. I did as you posted and it worked perfectly.
May 06, 2015 02:08 PM|Siva Krishna Macha|LINK
You're welcome. Please go through the entire article regarding Code-First migration. You may want to have complete overview about this topic, as you would need to know how do you plan the deployment strategy when you would deploy the applications on production
and maintaining them.