title: NullReferenceException when creating ObjectContext in Using statement
body: <p>Time once again to appeal to greater minds.</p>
  
  <p>I'm experiencing a very strange phenomenon. As the title states, I'm getting a NullReferenceException when trying to create an EF ObjectContext, but I only get the exception if I create the context within a Using statement. I've tried a variety of different ways, but it always ends up the same. And, of course, this is code that worked fine up until yesterday. It could possibly be relevant that my Windows Update ran yesterday morning.</p>
  
  <p>Anyway...</p>
  
  <p>If I try this</p>
  
  <pre><code>using (var context = new Entities(Env.Instance.Connection))
  {
      //do a bunch of EF stuff
  }
  </code></pre>
  
  <p>I get the NullReferenceException when creating my ObjectContext. Here Env.Instance.Connection is an EntityConnection that was created earlier in the program. I've stepped through to make sure the instance and EntityConnection both exist.</p>
  
  <p>If I do it this way</p>
  
  <pre><code>var context = new Entities(Env.Instance.Connection);
  //do a bunch of EF stuff
  context.Dispose();
  </code></pre>
  
  <p>Everything works fine.</p>
  
  <p>I've tried</p>
  
  <pre><code>using (var context = new Entities(Env.Instance.ConnectionName)) //the name of a connection string in my App.Config
  {
      //do a bunch of EF stuff
  }
  </code></pre>
  
  <p>I've tried</p>
  
  <pre><code>using (var context = new Entities(Env.Instance.ConnectionString)) //the actual connection string
  {
      //do a bunch of EF stuff
  }
  </code></pre>
  
  <p>I've even tried</p>
  
  <pre><code>using (var context = new Entities("metadata=res://*/MyDB.csdl|res://*/MyDB.ssdl|res://*/MyDB.msl;provider=System.Data.SqlClient;provider connection string=\"data source=MY-DB;initial catalog=MY-DB-PROD;persist security info=True;user id=dbuser;password=dbpass;multipleactiveresultsets=True;App=EntityFramework\""))
  {
      //do a bunch of EF stuff
  }
  </code></pre>
  
  <p>Doesn't matter. If I create the context within a using statement I always get a NullReferenceException.</p>
  
  <p>I can step into the code of my data layer and I exit the ObjectContext constructor</p>
  
  <pre><code>public Entities(string connectionString) : base(connectionString, "Entities")
  {
      this.ContextOptions.LazyLoadingEnabled = true;
      OnContextCreated();
  }
  </code></pre>
  
  <p>But, as soon as I exit the constructor, the error is thrown.</p>
  
  <p>Thoughts?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>In the absolute simplest iteration, the stack trace looks like so</p>
  
  <blockquote>
    <p>at IkaPlus.ViewModel.MainVM.ProcessMail3(Object sender,
    DoWorkEventArgs e) in C:\SVN\IkaPlus\IkaPlus\ViewModel\MainVM.cs:Line
    1371.<br>
       at IkaPlus.ViewModel.MainVM.RefillQueues() in C:\SVN\IkaPlus\IkaPlus\ViewModel\MainVM.cs:Line 832.</p>
  </blockquote>
  
  <p>Ideally, ProcessMail3 would be called from a BackgroundWorker, thus its signature, but at the moment, I'm calling it from the main thread with null parameters.</p>
  
  <p>To clarify, the ProcessMail3 method is <em>intended</em> to be called from a background worker, that's why it has sender and DoWorkEventArgs in its signature. But at the moment, I'm calling it directly from the main thread like so: ProcessMail3(null, null)</p>
  
  <p>I think rism is on to something.  If I do this</p>
  
  <pre><code>private void RefillQueues()
  {
      using (var context = new Entities("metadata=res://*/MyDB.csdl|res://*/MyDB.ssdl|res://*/MyDB.msl;provider=System.Data.SqlClient;provider connection string=\"data source=MY-DB;initial catalog=MY-DB-PROD;persist security info=True;user id=dbuser;password=dbpass;multipleactiveresultsets=True;App=EntityFramework\""))
      {
      }
  
      ProcessMail3(null, null);
  }
  
  private void ProcessMail3(object sender, DoWorkEventArgs e)
  {
      using (var context = new Entities("metadata=res://*/MyDB.csdl|res://*/MyDB.ssdl|res://*/MyDB.msl;provider=System.Data.SqlClient;provider connection string=\"data source=MY-DB;initial catalog=MY-DB-PROD;persist security info=True;user id=dbuser;password=dbpass;multipleactiveresultsets=True;App=EntityFramework\""))
      {
      }
  }
  </code></pre>
  
  <p>The using statement in RefillQueues works, but the using statement in ProcessMail3 does not.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>To simplify it further, I've eliminated the parameters in the signature of the offending method. So, now I'm calling</p>
  
  <pre><code>private void RefillQueues()
  {
      using (var context = new Entities("metadata=res://*/MyDB.csdl|res://*/MyDB.ssdl|res://*/MyDB.msl;provider=System.Data.SqlClient;provider connection string=\"data source=MY-DB;initial catalog=MY-DB-PROD;persist security info=True;user id=dbuser;password=dbpass;multipleactiveresultsets=True;App=EntityFramework\""))
      {
      }
  
      ProcessMail3();
  }
  
  private void ProcessMail3()
  {
      using (var context = new Entities("metadata=res://*/MyDB.csdl|res://*/MyDB.ssdl|res://*/MyDB.msl;provider=System.Data.SqlClient;provider connection string=\"data source=MY-DB;initial catalog=MY-DB-PROD;persist security info=True;user id=dbuser;password=dbpass;multipleactiveresultsets=True;App=EntityFramework\""))
      {
      }
  }
  </code></pre>
  
  <p>Same results. Using statement in the first method works. Using statement in the second method throws a NullReferenceException. There are no BackgroundWorkers running.</p>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>So, I seem to have found what's causing my error, though I still can't explain it.</p>
  
  <p>So, my ProcessMail3 method, actually looks like this (I've renamed it to EatCookies because...I like to eat cookies.)</p>
  
  <pre><code>private void EatCookies()
  {
      #region Empty the Queue
  
      string s = "Queue 3";
  
      CicConnector.CreateInstance(SettingsHandler.Instance.CIC_PASSWORD,
                "MYSERVER",
                "C:\\temp");
  
      //Do a bunch of stuff
  
      #endregion
  
      #region EF Stuff
  
      using (var context = new Entities("metadata=res://*/MyDB.csdl|res://*/MyDB.ssdl|res://*/MyDB.msl;provider=System.Data.SqlClient;provider connection string=\"data source=MY-DB;initial catalog=MY-DB-PROD;persist security info=True;user id=dbuser;password=dbpass;multipleactiveresultsets=True;App=EntityFramework\""))
      {
      }
  
      #endregion
  }
  </code></pre>
  
  <p>I haven't bothered to include the other lines of code because I've been stepping over them when I enter the method. Voodoo teching, I've found that if I comment out the line which creates my CiCConnector (shouldn't be relevant) my using statement after it works fine. If the line ISN'T commented out, regardless of whether I actually reach that line of code or not, the using statement doesn't work. If I set a breakpoint on the line where I create my string, and skip over the next line, going directly to my using statement, I get the NullReferenceException. If I comment out the CiCConnector line and do the same thing, the using statement works. And, again, if instead of a using statement, I simply create my ObjectContext then dispose of it manually later, it all works fine regardless of the CiCConnector line. This is all very strange.</p>
  
  <p><strong>EDIT 4</strong></p>
  
  <p>Oddly enough, the CiCConnector line does not cause the odd behavior if it's placed in the first method. So if I do</p>
  
  <pre><code>private void RefillQueues()
  {
      string s = "Queue 3";
  
      CicConnector.CreateInstance(SettingsHandler.Instance.CIC_PASSWORD,
                "MYSERVER",
                "C:\\temp");
  
      using (var context = new Entities("metadata=res://*/MyDB.csdl|res://*/MyDB.ssdl|res://*/MyDB.msl;provider=System.Data.SqlClient;provider connection string=\"data source=MY-DB;initial catalog=MY-DB-PROD;persist security info=True;user id=dbuser;password=dbpass;multipleactiveresultsets=True;App=EntityFramework\""))
      {
      }
  
      EatCookies();
  }
  
  private void EatCookies()
  {
      string s = "Queue 3";
  
      CicConnector.CreateInstance(SettingsHandler.Instance.CIC_PASSWORD,
                "MYSERVER",
                "C:\\temp");
  
      using (var context = new Entities("metadata=res://*/MyDB.csdl|res://*/MyDB.ssdl|res://*/MyDB.msl;provider=System.Data.SqlClient;provider connection string=\"data source=MY-DB;initial catalog=MY-DB-PROD;persist security info=True;user id=dbuser;password=dbpass;multipleactiveresultsets=True;App=EntityFramework\""))
      {
      }
  }
  </code></pre>
  
  <p>The using statement in the first method works fine, but in the second method it breaks.</p>
  
  <p><em>shrug</em> Your guess is as good as mine. Probably better. I think I'll just chalk it up as an oddity and simply not use the using statement. But, if anyone has any insight, I'd be thrilled to find out what the heck is going on here.</p>