Tôi đang cố gắng tạo danh sách thả xuống cho tên bộ phận. Tôi đang sử dụng MVC5. Tôi thấy quá nhiều giải pháp trên stack tràn nhưng tôi không bao giờ tìm thấy giải pháp có giá trị liên quan đến MVC5.Ngoại lệ loại 'System.InvalidOperationException' xảy ra trong EntityFramework.dll nhưng không được xử lý trong mã người dùng
Database Name : AppraisalDBContext
Table Name : Department
Column Name : deptID (Primarykey)
deptName (department name)
Description
Tôi nhận được lỗi này:
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:
điều khiển lớp Tên (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);
}
lớp Model Name (Sở. 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; }
}
}
lớp Model 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>
}
Cảm ơn lan. Liên kết này cũng hữu ích cho tôi: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an -asp-net-mvc-ứng dụng –