title: Automapper - ignoring while mapping
body: <p>Is it possible to ignore depending on destination value?</p>
  
  <p>Look what I want to do:      </p>
  
  <pre><code>object c;
  var key = ce.CreateEntityKey&lt;CustomerDataContract, Customer&gt;("FullCustomerSet", item, o =&gt; o.ID);
  
  if (ce.TryGetObjectByKey(key, out c))
  {
      Mapper.Map(item, (Customer)c);
  }
  else
  {
      c = Mapper.Map&lt;CustomerDataContract, Customer&gt;(item);
      ce.AddObject("FullCustomerSet", c);
  }
  </code></pre>
  
  <p>Ignore the <code>CreateEntityKey</code> part - it's just creating an <code>EntityKey</code> go get an item from EDM. <code>ce</code> is of type <code>ObjectContext</code>.</p>
  
  <p>What I want to do is try to get an entity from EDM, update it with AutoMapper or if it's not in there create it with AutoMapper. </p>
  
  <p>Problem is that AutoMapper maps all properties - even primary keys that are not allowed to be changed by edm and I get a nasty exception. I can ignore PKs in <code>CreateMap</code>, but then I'll not be able to do a 2nd part with creation. I get PKs from elsewhere... </p>
  
  <p>One thing I may consider is something in <code>CreateMap</code> - some conditional ignore on destination object or actually used method.
  Another is to apply some ignoring properties in <code>Mapper.Map</code> method, or manually mapping like so:</p>
  
  <pre><code>var map = Mapper.FindTypeMapFor&lt;TSource, TDestination&gt;();
  foreach (var item in map.GetPropertyMaps().OrderBy(o=&gt;o.GetMappingOrder()))
  {
      if (ignore.Contains(item.DestinationProperty.Name))
      {
          continue;
      }
      //and now I should do some mapping, but don't know how...
  }
  </code></pre>
  
  <p>Question is simple: How can I do it? :)</p>