title: Update is throwing DataException
body: <p>I'm using <code>async/await</code> on my <code>MVC</code> application and I get a <code>DataException</code> when calling <code>context.SaveChangesAsync()</code> (it even happens when not using async).</p>
  
  <p>I implemented unit of work pattern found <a href="http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application" rel="nofollow">here</a> and I have tried everything, changing code, using sync instead, commenting lines of calls, etc. I'm out of ideas. My entities won't update. But they do get created thought.</p>
  
  <p>Here's my piece of code:</p>
  
  <pre><code>// Inside POST EDIT
  try {
      if (ModelState.IsValid) {
          unitOfWork.StudentRepository.Update(student);
          await unitOfWork.SaveAsync();
          return RedirectToAction("Index");
      }
  } catch (DataException) {
      ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persist see your system administrator");
  }
  
  // Inside Repository -- It's a generic type defined as
  
  // public class GenericRepository&lt;TEntity&gt; : IGenericRepository&lt;TEntity&gt; 
  // where TEntity : class { .......
  
  public GenericRepository(SchoolContext context) {
      this.context = context;
      this.dbSet = context.Set&lt;TEntity&gt;();
  }
  
  public virtual void Update(TEntity entityToUpdate) {
      dbSet.Attach(entityToUpdate);
      context.Entry(entityToUpdate).State = EntityState.Modified;
  }
  
  // Inside UnitOfWork
  public Task&lt;int&gt; SaveAsync() {
      return context != null ? context.SaveChangesAsync() : Task.FromResult&lt;int&gt;(1);
  }
  </code></pre>
  
  <p><strong>Update:</strong> After doing so much research I found out people use a code snipped like this:</p>
  
  <pre><code>public void ApplyStateChanges()
  {
      foreach (var dbEntityEntry in ChangeTracker.Entries())
      {
          var entityState = dbEntityEntry.Entity as EntityBase;
          if (entityState == null)
              throw new InvalidCastException("All entites must implement the IObjectState interface, " +
                                             "this interface must be implemented so each entites state can explicitely determined when updating graphs.");
  
          dbEntityEntry.State = StateHelper.ConvertState(entityState.State);
      }
  }
  </code></pre>
  
  <p>and call it before <code>SaveChangesAsync()</code>, do that has anything to do with the problem I'm facing?</p>
  
  <p><strong>Update 2</strong>: Here's what I get when I click <a href="http://pastebin.com/AKaELhch" rel="nofollow">Copy exception detail to the clipboard</a>.</p>
  
  <p>I tried changing the code to use <code>TryToUpdateModel()</code> and it doesn't throw an exception, <strong>but the data isn't updated anyway</strong></p>