title: Automapper creating new instance rather than map properties
body: <p>This is a long one.</p>
  
  <p>So, I have a model and a viewmodel that I'm updating from an AJAX request. Web API controller receives the viewmodel, which I then update the existing model using AutoMapper like below:</p>
  
  <pre><code>private User updateUser(UserViewModel entityVm)
  {
      User existingEntity = db.Users.Find(entityVm.Id);
      db.Entry(existingEntity).Collection(x =&gt; x.UserPreferences).Load();
  
      Mapper.Map&lt;UserViewModel, User&gt;(entityVm, existingEntity);
      db.Entry(existingEntity).State = EntityState.Modified;
  
      try
      {
          db.SaveChanges();
      }
      catch
      { 
          throw new DbUpdateException(); 
      }
  
      return existingEntity;
  }
  </code></pre>
  
  <p>I have automapper configured like so for the User -> UserViewModel (and back) mapping.</p>
  
  <pre><code>Mapper.CreateMap&lt;User, UserViewModel&gt;().ReverseMap();
  </code></pre>
  
  <p>(Note that explicitly setting the opposite map and omitting the ReverseMap exhibits the same behavior)</p>
  
  <p>I'm having an issue with a member of the Model/ViewModel that is an ICollection of a different object:</p>
  
  <pre><code>[DataContract]
  public class UserViewModel
  {
      ...
      [DataMember]
      public virtual ICollection&lt;UserPreferenceViewModel&gt; UserPreferences { get; set; }
  }
  </code></pre>
  
  <p>The corresponding model is like such:</p>
  
  <pre><code>public class User
  {
      ...
      public virtual ICollection&lt;UserPreference&gt; UserPreferences { get; set; }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>The Problem:</strong></p>
  
  <p>Every property of the User and UserViewModel classes maps correctly, except for the <code>ICollections</code> of UserPreferences/UserPreferenceViewModels shown above. When these collections map from the ViewModel to the Model, rather than map properties, a new instance of a UserPreference object is created from the ViewModel, rather than update the existing object with the ViewModel properties.</p>
  
  <p>Model:</p>
  
  <pre><code>public class UserPreference
  {
      [Key]
      public int Id { get; set; }
  
      public DateTime DateCreated { get; set; }
  
      [ForeignKey("CreatedBy")]
      public int? CreatedBy_Id { get; set; }
  
      public User CreatedBy { get; set; }
  
      [ForeignKey("User")]
      public int User_Id { get; set; }
  
      public User User { get; set; }
  
      [MaxLength(50)]
      public string Key { get; set; }
  
      public string Value { get; set; }
  }
  </code></pre>
  
  <p>And the corresponding ViewModel</p>
  
  <pre><code>public class UserPreferenceViewModel
  {
      [DataMember]
      public int Id { get; set; }
  
      [DataMember]
      [MaxLength(50)]
      public string Key { get; set; }
  
      [DataMember]
      public string Value { get; set; }
  }
  </code></pre>
  
  <p>And automapper configuration:</p>
  
  <pre><code>Mapper.CreateMap&lt;UserPreference, UserPreferenceViewModel&gt;().ReverseMap();
  
  //also tried explicitly stating map with ignore attributes like so(to no avail):
  
  Mapper.CreateMap&lt;UserPreferenceViewModel, UserPreference&gt;().ForMember(dest =&gt; dest.DateCreated, opts =&gt; opts.Ignore());
  </code></pre>
  
  <p>When mapping a UserViewModel entity to a User, the ICollection of UserPreferenceViewModels is also mapped the User's ICollection of UserPreferences, as it should. </p>
  
  <p>However, when this occurs, the individual UserPreference object's properties such as "DateCreated", "CreatedBy_Id", and "User_Id" get nulled as if a new object is created rather than the individual properties being copied.</p>
  
  <p>This is further shown as evidence as when mapping a UserViewModel that has only 1 UserPreference object in the collection, when inspecting the DbContext, there are two local UserPreference objects after the map statement. One that appears to be a new object created from the ViewModel, and one that is the original from the existing model.</p>
  
  <p><strong>How can I make automapper update an existing Model's collection;s members, rather than instantiate new members from the ViewModel's collection? What am I doing wrong here?</strong></p>
  
  <p>Screenshots to demonstrate before/after Mapper.Map()</p>
  
  <p><a href="https://i.stack.imgur.com/DSYP1.png" rel="noreferrer"><img src="https://i.stack.imgur.com/DSYP1.png" alt="Before"></a></p>
  
  <p><a href="https://i.stack.imgur.com/XHFhe.png" rel="noreferrer"><img src="https://i.stack.imgur.com/XHFhe.png" alt="After"></a></p>