I have the latest 3.1.1 version of AutoMapper. For some reason the IsSourceValueNull when using ForAllMemebers doesn't seem to work or I am expecting a different result:
Here is an example of what I am trying to do. Please refrain from commenting on the DTOs looking exactly like the Entities. This is just an example of what I am running into with a more complex model.
public class User{
public int Id {get;set;}
public string UserName {get;set;}
public virtual int? ContactId {get;set;} //Foreign Key to contact object
public virtual Contact Contact {get;set;}
}
public class Contact {
public int Id {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
}
public class UserDto {
public int Id {get;set;}
public string UserName {get;set;}
public int? ContactId {get;set;} //Foreign Key to contact object
public ContactDto Contact {get;set;}
}
public class ContactDto {
public int Id {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
}
The code for the mapping looks something like this:
AutoMapper.Mapper.CreateMap<User,UserDto>().ForAllMembers(u => u.Condition(s => !s.IsSourceValueNull));
AutoMapper.Mapper.CreateMap<Contact,ContactDto>();
I am getting an error that the Source Value cannot be null. Meaning, the Contact is null coming back from the DB, which is OK, but AutoMapper is not running the condition for the Cotnact or ContactId. Both can be null in the DB. I have had to resort to checking if the source is null inside a ForMember block.