【发布时间】:2011-03-22 15:17:49
【问题描述】:
我使用实体框架代码优先方法创建了 MVC3 应用程序。我的模型很简单:
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int YearsAtCompany { get; set; }
}
上下文类是
public class EmployeeDB : DbContext
{
public DbSet<Employee> Employees { get; set; }
}
控制器看起来像这样:
EmployeeDB context = new EmployeeDB();
public ActionResult Index()
{
return View(context.Employees);
}
}
我已经创建了EmployeesDb.mdf 和Employee 表。
但我收到此错误:
The model item passed into the dictionary is of type 'System.Data.Entity.DbSet`1[DFEmployees.Models.Employee]', but this dictionary requires a model item of type 'DFEmployees.Models.Employee'.
[更新]
@model DFEmployees.Models.Employee
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
请提出解决方案。
【问题讨论】:
-
您可以发布您要导航到的视图的代码吗?
-
不确定,但是否可以将 View 键入为 Employee 而不是 IEnumerable
? -
@Jason:我已经添加了视图代码。请检查一下
标签: asp.net-mvc-3 entity-framework-4