【发布时间】:2016-11-21 14:19:58
【问题描述】:
我正在使用 objectDataSource 来填充 gridview。我有 2 个简单的课程:
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public int Salary { get; set; }
public Department Department { get; set; }
}
和
public class Department
{
public int Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
public List<Employee> Employees { get; set; }
}
我也有
public class EmployeeDBContext : DbContext
{
public DbSet<Department> Departments { get; set; }
public DbSet<Employee> Employees { get; set; }
}
现在在我的 EmployeeRepository 类中我有
public List<Department> GetDepartments()
{
EmployeeDBContext employeeDBContext = new EmployeeDBContext();
return employeeDBContext.Departments.Include("Employees").ToList();
}
即使我添加了 .Include("Employees"),gridview 中仍然缺少员工。我在这里做错了什么?
【问题讨论】:
-
您确定您得到的是一个空列表还是与数据源的绑定不起作用?你必须使用包含,因为延迟加载在没有虚拟的情况下不起作用
-
@BassamAlugili 我没有得到整个事情的空列表。我得到了结果,但是员工表中的数据丢失了!!
-
尝试延迟加载只需更改此行 public virual ICollection
Employees { get;放; } -
问题解决了没有?
-
不...我仍然得到相同的结果
标签: c# asp.net entity-framework gridview entity-framework-4