문제

Here is a small code snippet that illustrate the problem.

[TestMethod]
public void AutoMapper_MappingNullValue_ShouldMapNull()
{
    //Arrange
    var source = new DummySource { MyDate = null };
    var destination = new DummyDestination { MyDate = DateTime.Now };
    AutoMapper.Mapper.CreateMap<DummySource, DummyDestination>();

    //Act
    var returnResult = AutoMapper.Mapper.Map(source, destination);

    //Assert
    Assert.IsNull(returnResult.MyDate);
    Assert.IsNotNull(destination.MyDate);
}

private class DummySource
{
    public DateTime? MyDate { get; set; }
}

private class DummyDestination
{
    public DateTime? MyDate { get; set; }
}

How come this fail on the first assert? I expected to have a null value on the result. In fact, returnResult and destination are both with their initial value.

도움이 되었습니까?

해결책

Try updating the nuget package.

This was fixed in issue #310. The first version to implement the change is 3.0.0.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top