ASP.net MVC 5无法检索元数据

时间:2017-07-01 06:19:50

标签: c# asp.net asp.net-mvc entity-framework asp.net-mvc-5

我是ASP.net MVC 5的新手,我正在使用entityframework创建一个网站。我有两个表EmployeeDepartment。现在,当我创建一个名为DepartmentMembers的模型,在我的控制器Department Name中创建一个方法后,它将获得值Number of membersEmployeeController,这是我的代码

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添加视图时,我遇到了此错误

enter image description here

似乎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]属性后,因为此类未映射到我的表中,错误消息显示此

enter image description here

0 个答案:

没有答案