Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

I am trying to make dropdownlist for department name. I am using MVC5. I saw too much solution on stack overflow but I never found valuable solution related to MVC5.

Database Name : AppraisalDBContext
Table Name :  Department
Column Name : deptID (Primarykey)
              deptName (department name)
              Description 

I am getting 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 'AppraisalDBContext' context has changed since the database was created.Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?

Code:

Controller class Name (DepartmentController.cs):

public ActionResult Index(string depName)
{
    var DeptLst = new List<string>();          
    var GenreQry = from d in db.Department
                   orderby d.deptName
                   select d.deptName;

    DeptLst.AddRange(GenreQry);                // Here I am confusing
    ViewBag.depName = new SelectList(DeptLst); // Here I am confusing
    var depts = from m in db.Department        // Here I am confusing
                select m;


    return View(depts);
}

Model class Name (Departments.cs) :

namespace appraisalProject.Models
{
    [Table("Department")]
    public class Departments
    {
        [Key]
        public int deptId { get; set; }
        public string deptName { get; set; }
        public string Description { get; set; }
     }
}

Model class Name (AppraisalDBContext.cs) :

namespace appraisalProject.Models
{
    public class AppraisalDBContext:DbContext
    {
        public DbSet<Departments> Department { get; set; }
    }
}

Index.cshtml :

@using (Html.BeginForm())
{
<p>
    Genre: @Html.DropDownList("depts", "All")
    <input type="submit" value="Filter" />
</p>
}
like image 492
Anurag Jain Avatar asked Apr 17 '14 08:04

Anurag Jain


2 Answers

The error message details all that you need to know:

Additional information: The model backing the 'AppraisalDBContext' context has changed since the database was created.Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?

It means one of your classes used in the AppraisalDBContext has changed, but the database hasn't been updated so is now out of date. You need to update this using Code First migrations.

Here is a nice blogpost walking you through the process.

like image 66
Ian Avatar answered Oct 04 '22 01:10

Ian


lan's answer helped me. The problem was, i had updated the model classes but the database had still the old definition. I thought the Initializer ClearDatabaseSchemaIfModelChanges<> should reset the database on new calls, thats what i read on msdn. Turns out, the initializer is not called. So i dropped the database manually with:

new MyDbContext().Database.Delete();

in the WebApiConfig's register() Methode.

After that, the initializer is called on next startup and the db schema is updated.

like image 31
Kedu Avatar answered Oct 04 '22 00:10

Kedu