【发布时间】:2015-11-11 04:20:58
【问题描述】:
控制器
public ActionResult Index(int? id)
{
var userEmail = User.Identity.Name;
var model = db.Staffs.Where(i => i.Email == userEmail).Include("Histories").Include("CurrentApplications").FirstOrDefault();
return View(model);
}
var model = db.Staffs.Where(i => i.Email == userEmail).Include("Histories").Include("CurrentApplications").FirstOrDefault(); 行出现以下错误,但我不知道为什么会出现此错误。
错误
指定的包含路径无效。实体类型 “StaffPortalDBModel.Staff”没有声明导航属性 名称“历史”。
员工班级
public partial class Staff
{
public int StaffID { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public Nullable<decimal> AllocatedLeave { get; set; }
public Nullable<int> BalanceLeave { get; set; }
public virtual IEnumerable<History> Histories { get; set; }
public virtual IEnumerable<CurrentApplication> CurrentApplications { get; set; }
}
【问题讨论】:
-
最好使用可写集合(ICollection 等)而不是 IEnumerable
-
@eyeballs 为什么员工偏袒?
-
@M.Azad 当我将数据库添加到我的程序时,它是使用 edmx 自动生成的
-
为什么不使用 ViewModel?为什么使用包含?只需创建一个 ViewModel 并使用 Select => new xxx 封装您需要的内容
标签: asp.net-mvc include