我有一个基于Web的MVC 5项目。我首先使用ado实体模型数据库与EF6。 从我的模型我有以下T4生成的类
namespace Mvc5.Models
{
using System;
using System.Collections.Generic;
public partial class Department
{
public Department()
{
this.Employee2 = new HashSet<Employee2>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Employee2> Employee2 { get; set; }
}
}
和
namespace Mvc5.Models
{
using System;
using System.Collections.Generic;
public partial class Employee2
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public Nullable<int> DepartmentId { get; set; }
public virtual Department Department { get; set; }
}
}
And I have a DTO Class not Mapped to Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Mvc5.Models
{
public class DepartmentTotals
{
public string Name { get; set; }
public int Totals { get; set; }
}
}
在我的Controller中称为员工我有一个方法EmployeesByDepartment / 这将返回部门的员工视图
public ActionResult EmployeesByDepartment()
{
var departmentTotals = db.Employee2.Include("Department")
.GroupBy(x => x.Department.Name)
.Select(y => new DepartmentTotals
{
Name = y.Key,
Totals = y.Count()
}).ToList();
return View(departmentTotals);
}
问题是当我尝试使用DepartmentTotals作为模型
添加此方法的强类型视图时我收到以下错误
Error: There was an error running the selected code generator
Unable to retrieve metadata for DepartmentTotals
如何映射部门总计作为数据传输对象,我需要停止t4生成或使用旧版对象。所以我可以使用这个DTO查询数据库模型