我正在尝试使用导航属性,但是当我使用它时出现错误
值不能为空。
这是可以理解的,因为 People 集合是 NULL,但为什么它是 NULL?
我真正想做的是通过 RequestorPersonID 选择请求者的名称(最后一个代码片段的最后一行)
public abstract class Person
{
[Key]
public int PersonID { get; set; }
public string FirstName { get; set; }
}
public class Employee : Person
{
public string Department { get; set; }
}
public class FrDetail
{
[Key]
public int FrID { get; set; }
public int RequestorPersonID { get; set; }
virtual public IList<Person> People { get; set; }
}
public class EFDbContext : DbContext
{
public DbSet<Person> People { get; set; }
public DbSet<Employee> Employees { get; set; }
public DbSet<FrDetail> FrDetails { get; set; }
}
public ViewResult List()
{
EFDbContext context = new EFDbContext();
IQueryable<FrDetail> frDetails = context.FrDetails.Include(x => x.People);
return View(frDetails);
}
//The view
@model IQueryable<FrDetail>
@foreach (var p in Model)
Html.RenderPartial("FunctionRequestSummary", p);
}
//Partial View FunctionRequestSummary
@model FrDetail
@Model.People.Count()//IT'S ALWAYS ZERO
//@Model.People//NULL
@Model.People.Where(x=>x.PersonID==Model.RequestorPersonID).FirstOrDefault().FirstName
问题出现在计数始终为 0 的最后一行。我尝试过切换
ProxyCreationEnabled = 假;和 LazyLoadingEnabled = false;
这也没有帮助。我错过了什么吗?