title: Skip null values with custom resolver
body: <p>I want to use automapper to map between my public data contracts and my DB models. I have created a class which automatically goes through all the contracts are creates mappings. The only problem I have is that I only want to map values from the contract to the DB model if the value is not null. I have looked at other question on here but cant see examples that use custom resolvers.</p>
  
  <p>Here is some of my code</p>
  
  <pre><code>var mapToTarget = AutoMapper.Mapper.CreateMap(contract, mappedTo);
  foreach (var property in contract.GetProperties().Where(property =&gt; property.CustomAttributes.Any(a =&gt; a.AttributeType == typeof(MapsToProperty))))
  {
    var attribute = property.GetCustomAttributes(typeof(MapsToProperty), true).FirstOrDefault() as MapsToProperty;
  
    if (attribute == null) continue;
  
    mapToTarget.ForMember(attribute.MappedToName,
                      opt =&gt; 
                          opt.ResolveUsing&lt;ContractToSourceResolver&gt;()
                              .ConstructedBy(() =&gt; new ContractToSourceResolver(new MapsToProperty(property.Name, attribute.SourceToContractMethod, attribute.ContractToSourceMethod))));
  }
  
  
  private class ContractToSourceResolver : ValueResolver&lt;IDataContract, object&gt;
  {
    private MapsToProperty Property { get; set; }
  
    public ContractToSourceResolver(MapsToProperty property)
    {
       Property = property;
    }
  
    protected override object ResolveCore(IDataContract contract)
    {
       object result = null;
       if (Property.ContractToSourceMethod != null)
       {
           var method = contract.GetType()
                      .GetMethod(Property.ContractToSourceMethod, BindingFlags.Public | BindingFlags.Static);
            result = method != null ? method.Invoke(null, new object[] {contract}) : null;
        }
        else
        {
           var property =
                      contract.GetType().GetProperties().FirstOrDefault(p =&gt; p.Name == Property.MappedToName);
           if (property != null)
           {
               result = property.GetValue(contract);
           }
        }
  
        return result;
     }
  }
  </code></pre>
  
  <p>And this is how I want to use it</p>
  
  <pre><code>AutoMapper.Mapper.Map(dataContract, dbModel)
  </code></pre>
  
  <p>This currently works great but if there is a NULL value in the dataContract then it will replace the existing value in the dbModel, this is not what I want. How do I make AutoMapper ignore null source values?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>As pointed out in one of the answers there is this </p>
  
  <pre><code>Mapper.CreateMap&lt;SourceType, DestinationType&gt;().ForAllMembers(opt =&gt; opt.Condition(srs =&gt; !srs.IsSourceValueNull));
  </code></pre>
  
  <p>This would be ideal except for the fact that .ForAllMembers is not accessible from </p>
  
  <pre><code>Mapper.CreateMap(SourceType, DestinationType)
  </code></pre>