title: Update Exception with Entity-Framework?
body: <p><strong>Hi,</strong></p>
  
  <p>I am using EntityFramework for my ASP.NET MVC website but have some problems with the update.</p>
  
  <p>This is how my update code looka like :</p>
  
  <pre><code>using (BissEntities context = new BissEntities())
      {
  
        if (adCategoryFilter.Id &lt; 1)
          context.AddToAdCategoryFilter(adCategoryFilter);
        else
          context.Refresh(System.Data.Objects.RefreshMode.ClientWins, adCategoryFilter);
  
        if (context.SaveChanges() &gt; 0)
          return true;
      }
      return false;
  </code></pre>
  
  <p>When executing the context.Refresh i get the following exception :</p>
  
  <p><strong>The element at index 0 in the collection of objects to refresh has a null EntityKey property value or is not attached to this ObjectStateManager.</strong></p>
  
  <pre><code>Stacktrace :    at System.Data.Objects.ObjectContext.RefreshCheck(Dictionary`2 entities, Object entity, EntityKey key)
     at System.Data.Objects.ObjectContext.AddRefreshKey(Object entityLike, Dictionary`2 entities, Dictionary`2 currentKeys)
     at System.Data.Objects.ObjectContext.RefreshEntities(RefreshMode refreshMode, IEnumerable collection)
     at System.Data.Objects.ObjectContext.Refresh(RefreshMode refreshMode, Object entity)
     at Biss.Models.FilterModel.UpdateCategoryFilter(AdCategoryFilter adCategoryFilter) in C:\Users\Snowman\Documents\Visual Studio 2010\Projects\Biss\Biss\Models\FilterModel.cs:line 86 
  </code></pre>
  
  <p>This is not the first time I get this problem. First I thought that it might have to do with the relations in the database but after these was removed from the effected table the same exception remained.</p>
  
  <p><strong>Where does the adCategoryFilter come from?</strong></p>
  
  <p>The adCategoryFilter is instansiated(new) and then filled with data from the ViewObject(from the website). It does have the required data like filter Id (to map the filter to correct row in db).</p>
  
  <p>Pleas explain why Im getting this problem and how I could solve it.</p>
  
  <p><strong>BestRegards</strong></p>