我是ASP.net MVC 5的新手,我正在使用entityframework创建一个网站。我有两个表Employee
和Department
。现在,当我创建一个名为DepartmentMembers
的模型,在我的控制器Department Name
中创建一个方法后,它将获得值Number of members
和EmployeeController
,这是我的代码
public ActionResult DepartmentCount()
{
var employees = db.Employees.Include(e => e.Department)
.GroupBy(d => d.Department.Department_Name).Select(c => new DepartmentMembers
{
DepartmentName = c.Key,
MemberTotal = c.Count()
}).ToList().OrderByDescending(o => o.MemberTotal);
return View(employees.ToList());
}
现在,当我尝试使用模板List
和我的数据上下文类作为我的Connectionstring添加视图时,我遇到了此错误
似乎MVC假设我的班级DepartmentMembers
也是一张桌子。我怎么能做这个工作?
编辑1:这是我的部门成员
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVCusingEntityFramework.Models
{
[NotMapped]
public class DepartmentMembers
{
public string DepartmentName { get; set; }
public int MemberTotal { get; set; }
}
}
添加[NotMapped]
属性后,因为此类未映射到我的表中,错误消息显示此