EntityFramework.dll 中发生“System.InvalidOperationException”类型的异常,但未在用户代码中处理

2024-05-10

我正在尝试为部门名称制作下拉列表。我正在使用MVC5。我看到了太多关于堆栈溢出的解决方案,但我从未找到与 MVC5 相关的有价值的解决方案。

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

我收到此错误:

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:

控制器类名称(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);
}

模型类名称(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; }
     }
}

模型类名称(AppraisalDBContext.cs):

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

索引.cshtml:

@using (Html.BeginForm())
{
<p>
    Genre: @Html.DropDownList("depts", "All")
    <input type="submit" value="Filter" />
</p>
}

该错误消息详细说明了您需要了解的所有信息:

其他信息:自创建数据库以来,支持“AppraisalDBContext”上下文的模型已发生更改。请考虑使用 Code First 迁移来更新数据库(http://go.microsoft.com/fwlink/ http://go.microsoft.com/fwlink/?

这意味着您在中使用的课程之一AppraisalDBContext已更改,但数据库尚未更新,因此现已过时。您需要使用 Code First 迁移来更新此内容。

这是一个不错的blogpost http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx引导您完成整个过程。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

EntityFramework.dll 中发生“System.InvalidOperationException”类型的异常,但未在用户代码中处理 的相关文章

随机推荐