title: Can AutoMapper map to an attached entity?
body: <p>For my update operations, my controller action does something like this:</p>
  
  <pre><code>    public Task&lt;IHttpActionResult&gt; Update(int id, CustomerTypeDto customerTypeDto)
      {
          var entity = customerTypeService.Get(id);
  
          entity.Number = customerTypeDto.Number;
          entity.Description = customerTypeDto.Description;
  
          entity = customerTypeService.Save(entity);
  
          return Ok&lt;CustomerTypeDto&gt;(Mapper.Map&lt;CustomerTypeDto&gt;(entity));
      }
  </code></pre>
  
  <p>This is a simple example with a simple entity consisting of 2 properties. Most of my entities are complex entities having tons of properties and deep relationships.</p>
  
  <p>I need to load the entity from the DB first so that it starts being tracked by the context. I set the properties and then save the updated entity back into the DB. The repository's update method looks like this:</p>
  
  <pre><code>    public virtual void Update(T entity)
      {
          DbEntityEntry dbEntityEntry = dataContext.GetEntry(entity);
          if (dbEntityEntry.State == EntityState.Detached)
          {
              dbSet.Attach(entity);
              dbEntityEntry.State = EntityState.Modified;
          }
      }
  </code></pre>
  
  <p>Doing the above, EF correctly creates and Update SQL statement consisting of only the changes properties.</p>
  
  <p>With Automapper, is it possible to map the dto class to the model after it was loaded from db, so that changes can be tracked when the mapping occurs?</p>
  
  <p>Instead of doing:</p>
  
  <pre><code>entity.Number = customerTypeDto.Number;
  entity.Description = customerTypeDto.Description;
  </code></pre>
  
  <p>Is it possible to do:</p>
  
  <pre><code>entity = Mapper.Map(customerTypeDto, entity);
  </code></pre>
  
  <p>If this is not possible, what other alternatives are there apart from setting individual properties? Maybe using <a href="https://github.com/refactorthis/GraphDiff" rel="nofollow">GraphDiff</a> or this <a href="https://gist.github.com/benfoster/4488755" rel="nofollow">Delta implemenation</a>?</p>