title: Use AutoMapper for adding, updating and removing items in List
body: <p>Does AutoMapper not have a native approach to updating of nested lists where the instances need to be removed, added <em><strong>or updated</strong></em>?</p>
  <p>I am using AutoMapper in my ASP.Net Core application with EF Core to map my API resources to my models.  This has been working fine for most of my application, but I am not pleased with my solution for updating mapped nested lists where the listed instances need to persist.  I don't want to overwrite the existing list, I want to delete instances that are no longer in my incoming resource, add new instances, and update existing instances.</p>
  <p>The models:</p>
  <pre><code>public class MainObject
  {
      public int Id { get; set; }
      public List&lt;SubItem&gt; SubItems { get; set; }
      
  }
  
  public class SubItem
  {
      public int Id { get; set; }
      public int MainObjectId { get; set; }
      public MainObject MainObject { get; set; }
      public double Value { get; set; }
  }
  </code></pre>
  <p>The resources:</p>
  <pre><code>public class MainObjectResource
  {
      public int Id { get; set; }
      public ICollection&lt;SubItemResource&gt; SubItems { get; set; }
      
  }
  
  public class SubItemResource
  {
      public int Id { get; set; }
      public double Value { get; set; }
  }
  </code></pre>
  <p>The controller:</p>
  <pre><code>public class MainController : Controller
  {
      private readonly IMapper mapper;
      private readonly IMainRepository mainRepository;
      private readonly IUnitOfWork unitOfWork;
  
      public MainController(IMapper mapper, IMainRepository mainRepository, IUnitOfWork unitOfWork)
      {
          this.mapper = mapper;
          this.mainRepository = mainRepository;
          this.unitOfWork = unitOfWork;
      }
  
      [HttpPut(&quot;{id}&quot;)]
      public async Task&lt;IActionResult&gt; UpdateMain(int id, [FromBody] MainObjectResource mainObjectResource)
      {
          MainObject mainObject = await mainRepository.GetMain(id);
          mapper.Map&lt;MainObjectResource, MainObject&gt;(mainObjectResource, mainObjectResource);
          await unitOfWork.CompleteAsync();
  
          return Ok(); 
      }
  }
  </code></pre>
  <p>The mapping profile:</p>
  <pre><code>public class MappingProfile : Profile
  {
      public MappingProfile()
      {
          CreateMap&lt;MainObject, MainObjectResource&gt;();
          CreateMap&lt;SubItem, SubItemResource&gt;();
  
          CreateMap&lt;MainObject, MainObjectResource&gt;().ReverseMap()
          .ForMember(m =&gt; m.Id, opt =&gt; opt.Ignore())
          .ForMember(m =&gt; m.SubItems, opt =&gt; opt.Ignore())
          .AfterMap((mr, m) =&gt; {
                  //To remove
                  List&lt;MainObject&gt; removedSubItems = m.SubItems.Where(si =&gt; !mr.SubItems.Any(sir =&gt; si.Id == sir.Id)).ToList();
                  foreach (SubItem si in removedSubItems)
                      m.SubItems.Remove(si);
                  //To add
                  List&lt;SubItemResource&gt; addedSubItems = mr.SubItems.Where(sir =&gt; !m.SubItems.Any(si =&gt; si.Id == sir.Id)).ToList();
                  foreach (SubItemResource sir in addedSubItems)
                      m.SubItems.Add( new SubItem {
                          Value = sir.Value,
                      });
                  // To update
                  List&lt;SubItemResource&gt; updatedSubItems = mr.SubItems.Where(sir =&gt; m.SubItems.Any(si =&gt; si.Id == sir.Id)).ToList();
                  SubItem subItem = new SubItem();
                  foreach (SubItemResource sir in updatedSubItems)
                  {
                      subItem = m.SubItems.SingleOrDefault(si =&gt; si.Id == sir.Id);
                      subItem.Value = sir.Value;
                  }
              });
      }
  }
  </code></pre>
  <p>What I am doing here is a custom mapping, but I feel this is such a generic case that I expect AutoMapper to be able to handle this by some extension.  I have seen some examples where the mapping profile uses a custom mapping (.AfterMap), but then the actual mapping is done by a static instance of AutoMapper.  I am not sure if that is appropriate for my use of AutoMapper through dependency injection: I am not an experienced programmer but it doesn't seem sound to me.</p>