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 mapp
I've got the same exact issue with mapping up the conditionals to the non-generic types. Here's how I solved it.
Wire up:
foreach (PropertyInfo p in type.GetProperties().Where(x => x.GetCustomAttributes().Any()))
map.ForMember(p.Name, x => x.ResolveUsing(typeof(SkipMapIfNullResolver)).FromMember(p.Name));
The second .FromMember is required so the value for the member is passed to the value resolver, rather than the full model.
The resolver looks like this:
public class SkipMapIfNullResolver : IValueResolver
{
public ResolutionResult Resolve(ResolutionResult source)
{
if (source.Value == null)
source.ShouldIgnore = true;
return source;
}
}