title: Entity Framework 5 - DbContext Has Changes?
body: <p>I'm trying to find a way to determine of any changes have been made to a database context (DbContext).  Note: I'm using Visual Studio 2012 with Entity Framework 5 on a Windows 7, 64-bit box.</p>
  
  <p>Back when I used to use ObjectContext instead of DbContext, I could do something like:</p>
  
  <pre><code>public partial class MyObjectContext
  {
      public Boolean HasUnsavedChanges()
      {
          return (this.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Deleted).Any());
      }
  }
  </code></pre>
  
  <p>Now that I'm using DbContext, I tried to do this:</p>
  
  <pre><code>public partial class MyDbContext
  {
      public ObjectContext ObjectContext()
      {
          return (this as IObjectContextAdapter).ObjectContext;
      }
  
      public Boolean HasUnsavedChanges()
      {
          return (this.ObjectContext().ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Deleted).Any());
      }
  }
  </code></pre>
  
  <p>The problem that I'm having is that the method "HasUnsavedChanges()" always return "false" even when I know for a fact that the context has been changed.  Does anyone have any ideas as to what I'm doing wrong?</p>