本文介绍了实体框架,Linq,ObjectContext和延迟执行问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Linq对EntityFramework的新手,在Linq2EntityFramework中有一个问题是我或何时处理ObjectContext。我提出这个问题的原因是通常在我的DAL中,我有代码如

  public List< User> GetUsers()
{
using(MyEntities db = new MyEntities())// MyEntities继承ObjectContext。
{

//做一些linq select操作,返回IQueryable< User>
//在返回IQueryable时调用ToList(),当DB真正访问时,
//然后我们返回List< User>

}
//在使用语句之后调用ObjectContext上的Dispose方法,因此调用ObjectContext,反过来关闭我的DB连接并将其释放到池
}

现在,我不想这样做,我想让我的DAL返回IQueryable到我的BLL的列表,所以我的BLL可以对返回的IQueryable进行过滤,如Skip(),然后调用BLL中的ToList()。所以代码变成这样,

  public IQueryable< User> GetUsers()
{
//做一些linq select操作,返回IQueryable< User>
//然后只返回我们回来的
//注意:这里没有DB访问,这被称为延迟执行,因为真正的DB访问发生在BLL
}

然后在我的BLL中我有一个方法,如

  public List< User> GetUsers()
{
//获取IQueryable< User>从DAL到一个var说用户
return users.Skip(10).Take(20).ToList(); //这里DB访问真的发生了。注意:如果我们在DAL中使用,这里会抛出异常,说DB已经关闭了!
}

我的问题是,


  1. 如果我使用第二种方法,Linq2EF会在上述方法完成后知道关闭我的连接?

  2. 使用第二种方法,ObjectContext永远不会被处理那么在大型网站上会出现这个问题吗?

  3. 或者我如何获得IQuerayable,但仍然在DAL中处理ObjectContext?

非常感谢你,
Ray。



编辑:有一件事我不明白的是,如果连接是由ObjectContext智能管理的,那么只需不处理ObjectContext就可以了吗?

解决方案

有关您的问题:


  1. 它将关闭打开和关闭与ToList的连接,因为所有的枚举都在那个地方发生。只有放置这可以让你有一个长的打开的连接,如果你枚举它,并为每个项目运行一些半长的处理。

  2. 对于正常的大型网站和正常的操作,您将会确定。可以让您陷入困境的主要事项是实体跟踪,涉及您加载的任何实体。如果您正在加载太多信息或拥有非常大的网站,您可以避免使用它(不要将其用于正常网站)。

  3. 您可以使用您的数据访问类实现IDisposable,并在处理数据报文时处理数据报文。所以在您的GetUsers中,您可以在数据访问类中使用。


I'm new to Linq to EntityFramework, one question in Linq2EntityFramework I have is when or if to dispose the ObjectContext. The reason I ask this question is that normally in my DAL I have code like

public List<User>  GetUsers()
{
    using (MyEntities db = new MyEntities())   //where MyEntities inherits ObjectContext. 
    {

       // do some linq select operation which returns IQueryable<User>
       // call ToList() on the return IQueryable which is when the DB is really accessed
       // then we return List<User>

    } 
    // after using statement a Dispose method on ObjectContext is called, hence disposed the ObjectContext,  and in turn it closes my DB connection and releases it to the pool
}

Now, I don't want to do this, I want my DAL return IQueryable to my BLL instead of List, so that my BLL can do filtering like Skip() on the returned IQueryable then call ToList() in the BLL. So the code becomes like this,

public IQueryable<User>  GetUsers()
{
   // do some linq select operation which returns IQueryable<User>
   // then just return what we got back
   // Note: no DB access occurred here, this is called deferred execution, because the real DB access happens later in BLL
}

Then in my BLL I have a method like

public List<User>  GetUsers()
{
   // get IQueryable<User> from DAL to a var say users     
    return  users.Skip(10).Take(20).ToList(); // here the DB access really happens.  Note: if we put using in DAL, here will throw exception saying DB is already closed!!
}

My questions are,

  1. If I use the second approach, will Linq2EF know to close my connection after the above method finishes?
  2. With the second approach, ObjectContext is never Disposed, would this be a problem on large sites?
  3. Or how do I get IQuerayable back but still dispose the ObjectContext in DAL?

Thank you so much,Ray.

Edit: One thing I don't understand is that if the connection is smartly managed by the ObjectContext, then is it OK to just not disposing the ObjectContext? What other things beside the ObjectContext manage?

解决方案

Regarding your questions:

  1. It will close the open and close the connection with the ToList, since all the enumeration is happening at that place. Only place this can get you with a long opened connection, is if you enumerate over it and run some semi-long processing for each item.
  2. For normal large sites and normal operations, you will be ok with this. The main thing that can get you into trouble is entity tracking, which involves any entity you have loaded. If you are either loading too much information or have a very large site, you can avoid using it (don't sweat it for a normal site).
  3. You can have your data access class implement IDisposable, and dispose the datacontext when it is being disposed. So in your GetUsers you can put an using around your data access class instead.

这篇关于实体框架,Linq,ObjectContext和延迟执行问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:29