【问题标题】:navigation property is not loaded导航属性未加载
【发布时间】: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


【解决方案1】:

首先,您需要在 Employee 类中有一个外键 (DepartmentId)我不知道视频是怎么跑掉的。

public class Employee
{
    public int Id { get; set; }
    public int DepartmentId { get; set; } <=====
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Gender { get; set; }
    public int Salary { get; set; }
    public virtual Department Department { get; set; }
           ^^^^^^^
}

public class Department
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Location { get; set; }
    public virtual ICollection<Employee> Employees { get; set; }
           ^^^^^^^^^^^^^^^^^^
}

public partial class EmployeeDBContext : DbContext
{
    public virtual DbSet<Employee> Employee { get; set; }
    public virtual DbSet<Department> Department { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // Optional, but good practice to have the mapping here. 
        modelBuilder.Entity<Department>()
            .HasMany(e => e.Employee)
            .WithRequired(e => e.Department)
            .HasForeignKey(e => e.DepartmentId);
    }
}

- 或 -

为部门添加DepartmentId属性和[ForeignKey]数据注释。

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 int DepartmentId { get; set; } <=== 

    // Optional, but good practice to have this data annotation attribute. 
    [ForeignKey("DepartmentId")] <=== 
    public Department Department { get; set; }
}

仅供参考:您想使用虚拟,以防将来有人想使用延迟加载。

【讨论】:

  • 你认为他需要用流畅的 API 配置额外的实体吗?他首先在代码中有一对多的关系。
【解决方案2】:

你有没有尝试过这样的事情

return employeeDBContext.Departments.Include(x =&gt;x.Employees ).ToList();?

【讨论】:

    猜你喜欢
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    相关资源
    最近更新 更多