Sto tentando di creare un elenco a discesa per il nome del reparto. Sto usando MVC5. Ho visto troppa soluzione nello stack overflow ma non ho mai trovato una soluzione valida relativa a MVC5.Un'eccezione di tipo 'System.InvalidOperationException' si è verificata in EntityFramework.dll ma non è stata gestita nel codice utente
Database Name : AppraisalDBContext
Table Name : Department
Column Name : deptID (Primarykey)
deptName (department name)
Description
sto ottenendo questo errore:
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/?
Codice:
controller classe Nome (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);
}
classe Nome modello (Dipartimenti. 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; }
}
}
classe Nome modello (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>
}
Grazie lan. Questo link è anche utile per me: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an -Asp-net-MVC-applicazione –