title: Why is it required to reference EntityFramework.dll in the client project to make the DbContext IDisposable?
body: <p>Make a class library that has your Entity Framework model and object context. Then add a new console application to the solution. From the console app, reference the project that has your model.</p>
  
  <p>Now type this in the console app:</p>
  
  <pre><code>static void Main(string[] args)
  {
      using (var context = new ExperimentalDbContext())
      {
  
      }
      Console.ReadKey();
  }
  </code></pre>
  
  <p>When you build, you'll get an error that reports:</p>
  
  <blockquote>
    <p>The type 'System.Data.Entity.DbContext' is defined in an assembly that
    is not referenced. You must add a reference to assembly
    EntityFramework...yada yada yada...</p>
  </blockquote>
  
  <p>Now, I have done this many times over the last few years, but every time I get this error, I once again helplessly search the Internet for the solution that I have forgotten by then.</p>
  
  <p>The fix for this requires you to install the <strong>EntityFramework</strong> NuGet package in the <strong>ConsoleClient</strong> project.</p>
  
  <p>So, my question is not about what the fix is, but rather <em>why</em>? Because it doesn't make any sense!</p>
  
  <p>Just for the sake of completeness, I am using v6.1.3 of Entity Framework. But I have seen this error a number of times over the years with earlier versions as well.</p>
  
  <p><strong>Update</strong></p>
  
  <p>It appears that the problem is only when you use the <code>using</code> code block that is meant to call <code>Dispose</code> on <code>IDisposable</code>s.</p>
  
  <p>To test the hypothesis, create a console application, which references ClassLibrary1 in the same solution, which references ClassLibrary2 in the same solution, with the following code:</p>
  
  <pre><code>using ClassLibrary1;
  using System;
  
  namespace TestHypothesis1
  {
      class Program
      {
          // Testing the hypothesis presented in this answer: http://stackoverflow.com/a/38130945/303685
          // This seems to be the behavior with just (or may be even others I haven't tested for)
          // IDisposable.
          // anotherFoo instance is created just fine, but the moment I uncomment
          // the using statement code, it shrieks.
          static void Main(string[] args)
          {
              //using (var foo = new Foo())
              //{
              //    foo.Gar = "Gar";
  
              //    Console.WriteLine(foo.Gar);
              //}
  
              var anotherFoo = new Foo() { Gar = "Another gar" };
              Console.WriteLine(anotherFoo.Gar);
  
              Console.ReadKey();
          }
      }
  }
  
  
  using ClassLibrary2;
  using System;
  
  namespace ClassLibrary1
  {
      public class Foo: Bar, IDisposable
      {
          public string Gar { get; set; }
  
          public void Dispose()
          {
              throw new NotImplementedException();
          }
      }
  }
  
  
  namespace ClassLibrary2
  {
      public class Bar
      {
          public string Name { get; set; }
      }
  }
  </code></pre>
  
  <p>And you will observe that the compiler complains about the missing reference only for the instantiation of the first <code>Foo</code> and not for the second instance.</p>
  
  <p>Strangely though, in the first EntityFramework example, if you removed the reference to EntityFramework.dll from the console application and changed the code in the <code>Main</code> to this, it still complains about the missing reference.</p>
  
  <pre><code>static void Main(string[] args)
  {
      var context = new ExperimentalDbContext();
      Console.ReadKey();
      context.Dispose();
  }
  </code></pre>
  
  <p>Additionally, if you comment out the call to <code>context.Dispose()</code>, the last line of the code snippet above, the code still works fine even though it throws an <code>InvalidOperationException</code> but that, I surmise, is due to a race condition of the context being disposed before its iterator completes its <code>MoveNext</code> call.</p>
  
  <pre><code>static void Main(string[] args)
  {
      var context = new ExperimentalDbContext();
      Console.ReadKey();
      // context.Dispose();
  }
  </code></pre>
  
  <p>So, the new additional question now becomes:</p>
  
  <p><strong>What is it about the way the <code>using</code> statement is implemented that makes the compiler stop in its tracks in linking references?</strong></p>
  
  <p>The original question also remains.</p>
  
  <p><strong>Yet another update</strong></p>
  
  <p>It now appears that the problem may be zeroed down further to the call to the <code>IDisposable.Dispose</code> method and the problem is therefore not with the implementation of the <code>using</code> statement. The <code>using</code> statement just seems to be an innocent assurance that <code>Dispose</code> will be called and nothing else.</p>
  
  <p>Therefore, in the above <code>Foo</code> example, if you insert a call to <code>anotherFoo.Dispose</code> at the end, the compiler starts to complain again. Like so:</p>
  
  <pre><code>using ClassLibrary1;
  using System;
  
  namespace TestHypothesis1
  {
      class Program
      {
          // Testing the hypothesis presented in this answer: http://stackoverflow.com/a/38130945/303685
          // This seems to be the behavior with just (or may be even others I haven't tested for)
          // IDisposable.
          // anotherFoo instance is created just fine, but the moment I uncomment
          // the using statement code, it shrieks.
  
          // Final update:
          // The trigger for the error seems to be the call to the Dispose method and not
          // particularly the implementation of the using statement, which apparently, simply
          // ensures that Dispose is called, as is also well-known and documented.
          static void Main(string[] args)
          {
              //using (var foo = new Foo())
              //{
              //    foo.Gar = "Gar";
  
              //    Console.WriteLine(foo.Gar);
              //}
  
              var anotherFoo = new Foo() { Gar = "Another gar" };
              Console.WriteLine(anotherFoo.Gar);
  
              anotherFoo.Dispose();
  
              Console.ReadKey();
          }
      }
  }
  </code></pre>
  
  <p>So, the final question, then, in summary is this:</p>
  
  <p><strong>Why is it that the call to <code>Dispose</code> stops the compiler from being able to link assembly references?</strong></p>
  
  <p>I think we are now getting somewhere.</p>