简体   繁体   English

IQueryable 在使用后被释放

[英]IQueryable is disposed after using

I have some short code that looks like this:我有一些看起来像这样的短代码:

public static IQueryable<User> SelectFromEmployee(int employee)
{
    using (var ctx = Database.AccountingContext())
    {
        return ctx.Users.Where(c => c.Employee_FK == employee);
    }
}

If i just keep this code as it is and use the result i get en exception telling me that the data is disposed.如果我只是保持此代码原样并使用结果,我会得到一个异常,告诉我数据已被处理。 But if I do like this:但如果我这样做:

public static IEnumerable<User> SelectFromEmployee(int employee)
{
    using (var ctx = Database.AccountingContext())
    {
        return ctx.Users.Where(c => c.Employee_FK == employee).ToList();
    }
}

Everything works just fine.一切正常。 But my problem is that i want to use Linq Dynamic that require IQueryable.但我的问题是我想使用需要 IQueryable 的 Linq Dynamic。 Is there any way to return a local IQueryable so i can continue working with it?有什么方法可以返回本地 IQueryable 以便我可以继续使用它?

You need to choose:您需要选择:

  1. Not disposing the context(without the using statement) and getting IQueryable<User> , which is relied on Database.不处理上下文(没有 using 语句)并获取依赖于数据库的IQueryable<User>
  2. Disposing the context and getting IQueryable<User> through ToList().AsQueryable() , which is relied on Memory.通过ToList().AsQueryable()处理上下文并获取IQueryable<User> ,这依赖于 Memory。

Please note that the important point is where data is through the deferred loading.请注意,重要的一点是数据通过延迟加载的位置。

The problem is that you are creating your Data Context and then Disposing it:问题是您正在创建数据上下文,然后将其处置:

using (var ctx = Database.AccountingContext())

Just to get your going, try this instead:只是为了让你继续,试试这个:

ObjectContext context = Database.AccountingContext();
public static IQueryable<User> SelectFromEmployee(int employee)
{
    return context.Users.Where(c => c.Employee_FK == employee);
}

What I did here is that I moved the Data Context outside of the method instead, the problem now is that you need to control the dispose of it by yourself and you might also need to consider that you are blocking for more connections, so be sure to dispose the connection.我在这里所做的是我将数据上下文移到了方法之外,现在的问题是您需要自己控制它的处置,您可能还需要考虑您正在阻止更多连接,所以请确保处置连接。

Edit编辑

I assumed that you wanted to be able to use relations etc as well.我假设您也希望能够使用关系等。

The problem is you are disposing the DataContext.问题是您正在处理 DataContext。 One option is to keep it while you use the query result.一种选择是在使用查询结果时保留它。

If you want to literally do what you're asking如果你想按字面意思做你所要求的

public static IQueryable<User> SelectFromEmployee(int employee)
{
    using (var ctx = Database.AccountingContext())
    {
        return ctx.Users.Where(c => c.Employee_FK == employee).ToList().AsQueryable();
    }
}

beware though that what you return is no longer associated with a data context, so relationship navigation for example, will not work as you might expect if the data context had not been disposed.请注意,虽然您返回的内容不再与数据上下文相关联,但例如,如果数据上下文尚未处理,则关系导航将无法正常工作。

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

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