1

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.

DDiVita
  • 4,225
  • 5
  • 63
  • 117
  • `IsSourceValueNull` should be a property of User. What does it do? – Gert Arnold Feb 19 '14 at 08:51
  • @GertArnold, I am not sure I follow. IsSourceValueNull is a condition in AutoMapper. I stated that I am getting an error that the Source Value cannot be null. ContactId and Contact are both properties on a User – DDiVita Feb 19 '14 at 13:50
  • You're right, `u` is a `ResolutionContext` in the other overload. I don't know what's going on here, but wouldn't you want to check for `IsSourceValueNull` of specific properties? – Gert Arnold Feb 20 '14 at 21:52

0 Answers0