title: Get DbContext from Entity in Entity Framework
body: <p>I'm deep somewhere in the Business layer in a debugging session in Visual Studio trying to figure out why an Entity is behaving strangely when trying to persist the changes.</p>
  
  <p>It would really be helpful to get a reference to the DbContext this Entity belongs to, at this point in the call stack.</p>
  
  <p>I.e. to see what the state is of this Entity is (Unchanged, Modified, etc).</p>
  
  <p>So I'm looking for a helper method like this:</p>
  
  <pre><code>var db_context = DbContextHelpers.GetDbContext(entity);
  
  // after that I could do something like this
  var state = db_context.Entry(entity);
  </code></pre>
  
  <p>I can use this stuff in the Immediate window during debugging.</p>
  
  <p>Anyone any suggestions?</p>
  
  <p><strong>Extra notes</strong></p>
  
  <p>The Entity must be aware of the <code>DbContext</code> somewhere, because it is using it for lazy loading navigation properties?</p>