实体框架-MultipleActiveResultSets对缓存的影响 [英] Entity Framework - Affect of MultipleActiveResultSets on Caching

查看:85
本文介绍了实体框架-MultipleActiveResultSets对缓存的影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个类似于以下内容的类。有一个线程使用Entity Framework Code First DbContext做一些工作。

So I have a Class that looks something like the following. There is a thread that does some work using an Entity Framework Code First DbContext.

我遇到的问题是,即使应该为每个处理循环处理并重新创建m_DB上下文,它似乎也在缓存数据。

The problem I'm having is that it seems like m_DB context is caching data even though it should be disposed and recreated for every processing loop.

我看到的是,在加载的模型中没有关系中的某些数据。如果我突然终止并重新启动该过程,则会找到数据,就像应该的一样。

What I've seen is that some data in a relationship isn't present in the models loaded. If I kill and restart the process suddenly the data is found just like it should.

我唯一能想到的是该应用程序在该应用程序中使用MultipleActiveResultSets = true数据库连接字符串,但是我找不到任何明确说明这将导致我看到的行为的东西。

The only thing I can think of is this app is using the MultipleActiveResultSets=true in the database connection string, but I can't find anything stating clearly that this would cause the behavior I'm seeing.

任何见解都会受到赞赏。

Any insight would be appreciated.


public class ProcessingService
{

  private MyContext m_DB = null
  private bool m_Run = true;

  private void ThreadLoop()
  {
    while(m_Run)
    {
      try
      {
        if(m_DB == null)
          m_DB = new MyContext();
      }
      catch(Exception ex)
      {
        //Log Error
      }
      finally
      {
        if(m_DB != null)
        {
          m_DB.Dispose();
          m_DB = null;
        }
      }
    }
  }

  private void ProcessingStepOne()
  {
    // Do some work with m_DB
  }

  private void ProcessingStepTwo()
  {
    // Do some work with m_DB
  }
}


推荐答案

多个活动结果集或MARS是SQL 2005/2008的功能和ADO.NET,其中一个连接可以由多个活动结果集使用(正如名称所暗示的)。尝试关闭此连接字符串并观察该应用程序的行为,我想这可能是造成您问题的原因。阅读以下MSDN链接以获取有关MARS的更多信息

Multiple Active Result Sets or MARS is a feature of SQL 2005/2008 and ADO.NET where one connection can be used by multiple active result sets (Just as the name implies). try switching this off on the connection string and observe the behaviour of the app, i am guessing that this could be the likely cause of your problem. read the following MSDN link for more on MARS

MSDN-多个活动结果集

编辑:
尝试:

Try:

var results = context.SomeEntitiy.AsNoTracking() where this = that select s;

AsNoTracking()关闭实体的内部更改跟踪,并且还应强制实体框架重新加载实体时间。

AsNoTracking() switches off internal change tracking of entities and it should also force Entity Framework to reload entities every time.

无论怎么说,您都需要进行一些重构,因为代码中显然存在设计缺陷。

Whatever said and done you will require some amount of re-factoring since there's obviously a design flaw in your code.

这篇关于实体框架-MultipleActiveResultSets对缓存的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆