This is probably a simple answer, but i’m just starting to get the hand of AutoMapper.
Anyway, i have a domain object like this:
public class User
{
public string Name { get; set; }
public FacebookUser FacebookUser { get; set; }
}
And a ViewModel like this:
public class UserViewModel
{
public string Name { get; set; }
public long FacebookUniqueId { get; set; }
}
Here’s what i have in my AutoMapper configuration:
Mapper.CreateMap<User,UserViewModel>()
.ForMember(dest => dest.FacebookUniqueId, opt => opt.MapFrom(src => src.FacebookUser.FacebookUniqueId))
But it throws an exception when the FacebookUser object is null, which is to be expected.
How do i tell AutoMapper:
Map UserViewModel.FacebookUniqueId to User.FacebookUser.FacebookUniqueId, except for when it’s null, then use
0.
Any ideas?
Yikes, if I had just checked if the first idea worked, thought the
MapFrommethod only took an expression that pointed to a property to resolve it that way…