title: Entity Framework Code First Updating with Where Clause
body: <p>I am using <a href="http://patrickdesjardins.com/blog/using-a-filtereddbset-with-entity-framework-to-have-dynamic-filtering" rel="nofollow">FilteredSet</a> to have automatically data from the current logged user of my system. It provides me a dynamic way to return data from the current user and not access other users data.</p>
  
  <p>I would like to do the same when I am updating an entity. I could get the entity from the database with the FilteredSet and if the entity id doesn't belong the the user thrown an exception BUT I prefer not to load the entity first and simply attach this one and update.</p>
  
  <p>So far, I have try:</p>
  
  <pre><code>public void UpdateOwnable&lt;TEntity&gt;(TEntity entity) where TEntity : class, IEntity, IUserOwnable
  {
         var entityLocal = SetOwnable&lt;TEntity&gt;().Local.SingleOrDefault(d =&gt; d.Id == entityId);
         if (entityLocal==null)
         {
            entityLocal = this.SetOwnable&lt;TEntity&gt;().Attach(entity);
         }
         this.ChangeTracker.Entries&lt;TEntity&gt;().Single(d =&gt; d.Entity == entityLocal).State = EntityState.Modified;
  }
  </code></pre>
  
  <p>The problem is that the query generated doesn't include a WHERE clause. I would expect to find a way that the Where clause is not only the primary key of the entity but also the user id. </p>
  
  <pre><code>Update Workout
  SET name ..
  WHERE id = 1
  AND idUser = 123;
  </code></pre>
  
  <p>This SQL query is what I would like to have Entity Framework to generate. With an "AND" clause for the idUser. </p>
  
  <p>Can this be possible?</p>