'System.InvalidOperationException'类型的异常出现在EntityFramework.dll但在用户code没有处理 [英] An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
问题描述
我试图做出部门名称的DropDownList。我使用MVC5。我看到堆栈溢出太多的解决办法,但我从来没有发现相关MVC5有价值的解决方案。
数据库名称:AppraisalDBContext
表姓名:部门
列名:DEPTID(PrimaryKey的)
DEPTNAME(部门名称)
描述
我得到这个错误:
'System.InvalidOperationException'类型的异常出现在EntityFramework.dll但在用户code没有处理其他信息:该模型支持了AppraisalDBContext上下文自数据库created.Consider使用code首先迁移到更新数据库(http://go.microsoft.com/fwlink/改变了吗?
code:
控制器类名(DepartmentController.cs):
公众的ActionResult指数(字符串depName)
{
VAR DeptLst =新的List<串GT;();
VAR GenreQry从D =在db.Department
排序依据d.deptName
选择d.deptName; DeptLst.AddRange(GenreQry); //我在这里混淆
ViewBag.depName =新的SelectList(DeptLst); //我在这里混淆
从M个变种科指南=在db.Department //我在这里混淆
选择米;
返回查看(科指南);
}
示范类名(Departments.cs):
命名空间appraisalProject.Models
{
[表(部门)]
公共类部门
{
[键]
公众诠释DEPTID {搞定;组; }
公共字符串DEPTNAME {搞定;组; }
公共字符串描述{搞定;组; }
}
}
示范类名(AppraisalDBContext.cs):
命名空间appraisalProject.Models
{
公共类AppraisalDBContext:的DbContext
{
公共DbSet<部门及GT;部{搞定;组; }
}
}
Index.cshtml:
@using(Html.BeginForm())
{
&所述p为H.;
类型:@ Html.DropDownList(科指南,全部)
<输入类型=提交值=过滤器/>
&所述; / P>
}
该错误消息的详细信息所有你需要知道的:
更多信息:因为数据库是created.Consider使用code首先迁移到更新数据库(的 http://go.microsoft.com/fwlink/ ?
块引用>这意味着在
AppraisalDBContext
已经改变使用您的类之一,但数据库尚未更新,所以现在已经过时了。您需要使用code首先迁移到更新此。下面是一个不错的<一个href=\"http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-$c$c-based-migrations-walkthrough.aspx\"相对=nofollow>博文中走您完成整个过程。
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> }
解决方案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.
这篇关于'System.InvalidOperationException'类型的异常出现在EntityFramework.dll但在用户code没有处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!