我正在尝试获得单个员工记录,但是查看诊断工具,它显示实体框架执行大量查询加载整个数据库。延迟加载已启用,我正在使用public
和virtual
关键字,因此我不认为这应该是问题所在。有什么我遗漏的,Employee
记录的导航属性不应该加载。
服务:
return _employeeRepo.GetEmployee(sid);
存储库:
public Employee GetEmployee(string sid)
{
Employee employee = Context.Employees.SingleOrDefault(e => e.SID == sid);
return employee != null ? employee.ToDomain() : null;
}
员工模型:
public class Employee
{
...
public virtual ICollection<Address> Addresses { get; set; }
public virtual ICollection<Disability> Disabilities { get; set; }
...
public virtual Bureau Bureau { get; set; }
public virtual Division Division { get; set; }
...
public Domain.Models.Employee ToDomain()
{
return Mapper.Map<Domain.Models.Employee>(this);
}
}
上下文:
public class SqlContext : DbContext
{
public SqlContext() : base("SqlContext")
{
Database.SetInitializer<SqlContext>(null);
}
public virtual DbSet<EfModels.Address> Addresses { get; set; }
public virtual DbSet<EfModels.Bureau> Bureaus { get; set; }
public virtual DbSet<EfModels.Disability> Disabilities { get; set; }
public virtual DbSet<EfModels.Division> Divisions { get; set; }
public virtual DbSet<EfModels.Employee> Employees { get; set; }
}
答案 0 :(得分:1)
您的地图工具(AutoMapper)就是问题所在。
当您调用employee.ToDomain()时,正在访问您实体的导航属性,导致EF延迟加载表。