0

I am learning Basics of ASP.NET MVC. And I created a model view controller for a page. In my project I have instructor table and courses table. Once I ran the project and everything seemed to be working fine. But then I wanted to edit the model; like adding one more field in the instructor table present in courses.cs file. As soon as I did it one exception started coming stating about some context change and migration issue.

Error Message:

the context has changed since the database was created. Consider using Code First Migrations to update the database

I found that I need to add Database.SetInitializer(null); in global.asax file. Even after making that change I am still stuck with the same error. My question is since I am adding a field in courses.cs file by writing public string address_ { get; set; } . Do I need to manually add corresponding code in the view also. During initial solution views got created automatically with table columns (original) in it. But since I have added one extra column in the instructor table in courses.cs file. Do I need to manually add that particular field in the corresponding view in "indexcs.html" file? If no, then why am I still getting error. If yes, then do I have to manually open all the view related to courses.cs and start making changes manually. Because in courses view I have many .cshtml files. Do I need add at all the places respective changes related to me adding one extra field i.e, public string address_ { get; set; }. Is there any automatic way to do it. Like the way visual studio did it in the first time I made the controller.

ekad
  • 14,436
  • 26
  • 44
  • 46
tanz
  • 627
  • 1
  • 10
  • 21
  • How did you generate the database from your model? Migrations is about making sure the database matches up with your model, it has nothing at all to do with the views or .cshtml files. Also, it's generally advised that you include the **actual** error message and a snippet of code in your question; not doing so will quickly earn you downvotes or worse, close votes. – Claies Mar 14 '15 at 04:35
  • Since I have added one extra line of code public string address_ { get; set; } . Do I have to manually change the views also. – tanz Mar 14 '15 at 04:49
  • that error further confirms, you need to update your database to match your model. Again, this has **nothing** to do with your views. – Claies Mar 14 '15 at 04:49
  • https://msdn.microsoft.com/en-us/data/jj591621.aspx – Claies Mar 14 '15 at 04:51
  • in fact, the error actually lists this link at the end. Read the article, it explains what is going on and how to manage it much better than we could in an answer here. – Claies Mar 14 '15 at 04:52
  • [http://stackoverflow.com/questions/3600175/the-model-backing-the-database-context-has-changed-since-the-database-was-crea] check this link. Here its been suggested to add Database.SetInitializer(null); to get rid of migration issue – tanz Mar 14 '15 at 04:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72967/discussion-between-tanz-and-claies). – tanz Mar 14 '15 at 04:53
  • well that article isn't appropriate for your situation, unless you want to modify the database by hand. – Claies Mar 14 '15 at 04:53
  • @Claies : I will pay heed to your suggestion and will look into the link. Thanks! – tanz Mar 14 '15 at 05:08

0 Answers0