title: Entity framework code first update on detached objects
body: <p>How do I update a model and its child by using attach with specific objects?</p>
  
  <p>I use repository pattern that looks like this with old entity framework:</p>
  
  <pre><code>    public User UpdateUser(User user, IEnumerable&lt;System.Linq.Expressions.Expression&lt;Func&lt;User, object&gt;&gt;&gt; properties)
      {
          if (user.UserId == 0)
          {
              db.Users.Attach(user);
          }
          else
          {
              db.Users.Attach(user);
              ObjectStateEntry entry = db.ObjectStateManager.GetObjectStateEntry(user);
              foreach (var selector in properties)
              {
                  string propertyName = PropertyToString(selector.Body);
                  entry.SetModifiedProperty(propertyName);
              }
          }
  
          context.SaveChanges();
          return user;
      }
  
      private static string PropertyToString(Expression selector)
      {
          switch (selector.NodeType)
          {
              case ExpressionType.MemberAccess:
                  return ((selector as MemberExpression).Member as PropertyInfo).Name;
                  break;
  
              case ExpressionType.Convert:
                  //Needed to recognize bool datatype
                  return (((selector as UnaryExpression).Operand as MemberExpression).Member as PropertyInfo).Name;
  
                  break;
          }
          throw new InvalidOperationException();
      }
  </code></pre>
  
  <p>But now, code-first does not support <code>db.ObjectStateManager</code>. How can I run an update on specific properties for detached objects?</p>