Entity Framework - Affect of MultipleActiveResultSets on Caching

c# entity-framework

Question

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.

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.

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
  }
}

1
6
10/24/2013 11:22:04 PM

Popular Answer

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 - Multiple Active Result Sets

Edit: Try:

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

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.

3
7/7/2016 2:04:42 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow