简体   繁体   English

急切加载在EF 6中不起作用

[英]Eager loading not working in EF 6

I am having a method that returns me a list of employees. 我有一种方法可以向我返回员工列表。

public Employee GetAdminTypeAndPermission(int userID)
{
    using (var unitOfWork = new EFUnitOfWork())
    {
        var employeeRepo =
            new EmployeeRepository(new EFRepository<Employee>(), unitOfWork);

        DbSet<Employee> employeeObjSet =
            ((EmployeeEntities)employeeRepo.Repository.UnitOfWork.Context).Employees;

        return employeeObjSet.Where(emp => emp.FK_UserID == userID).
            Include("AdminType.Permissions").FirstOrDefault();
    }
}

In another method I am trying to use like, 我正在尝试使用另一种方法,例如

var employee= GetAdminTypeAndPermission(loginInfo.UserID);
var permission = employee.AdminType.Permissions.FirstOrDefault();

I am getting an error at the following line: 我在以下行出现错误:

var permission = employee.AdminType.Permissions.FirstOrDefault();

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection. ObjectContext实例已被处置,不能再用于需要连接的操作。 I understand the Context object disposes after the end of using block but I am using Eager loading to access the Navigation properties and I should get the result set. 我了解Context对象在使用完块之后处理,但是我正在使用Eager加载来访问Navigation属性,因此我应该得到结果集。

Please help me in understanding what mistake I am doing here and how should I correct. 请帮助我了解我在这里犯了什么错误以及如何纠正。

Any help is highly appreciated. 非常感谢您的帮助。 Thanks in advance. 提前致谢。

You have your DbContext in a using statement: 您在using语句中拥有DbContext:

using (var unitOfWork = new EFUnitOfWork())

Once you get out of this using statement, your connection to the database is effectively closed. 一旦您摆脱了using语句,您与数据库的连接就会有效关闭。 Either: 要么:

  • Query the information you need while inside your function (and inside your using statement) or 在函数内(和using语句内)查询所需的信息,或者
  • Pass in the DbContext to the method so you can continue to use it: 将DbContext传递给方法,以便您可以继续使用它:

     using (var unitOfWork = new EFUnitOfWork()) { var employee= GetAdminTypeAndPermission(loginInfo.UserID, unitOfWork); //DbContext passed in var permission = employee.AdminType.Permissions.FirstOrDefault(); } 

Well, maybe you got the related table name wrong? 好吧,也许您弄错了相关的表名? What I mean, stringly typed stuff is hard to control in your code, ie is it maybe (Permission without the last 's'?): 我的意思是, 字符串类型的内容很难在您的代码中进行控制,也就是说,它是否可能(没有最后一个“ s”的权限?):

return employeeObjSet.Where(emp => emp.FK_UserID == userID).
            Include("AdminType.Permission").FirstOrDefault();

For this kind of error I would advise you to use the lambda extension property name resolver, ie the overload/extension of the Include method which will be included if you enter the following using directive: 对于这种错误,我建议您使用lambda扩展属性名称解析程序,即,如果您输入以下using指令,则将包含Include方法的重载/扩展:

using System.Data.Entity;

This is the extension I tend to use: 这是我倾向于使用的扩展名:

public static IQueryable<T> Include<T, TProperty>(this IQueryable<T> source, Expression<Func<T, TProperty>> path);

It's used like this: 它的用法如下:

return employeeObjSet.Where(emp => emp.FK_UserID == userID).
        Include(x => x.Permissions).FirstOrDefault();

Here its x.Permission s because of the ICollection pluralization of the ef generator or your cf coding standard you use. 这里的x.Permission 因为外汇基金产生的ICollection的多元化,或者你的CF编码您使用标准。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM