0

当我升级到 Automapper 5.1.1 时,使用只读属性定义的集合的映射停止工作(使用 Automapper 4.2.1 可以正常工作)

这是一个示例代码,您可以尝试使用两个版本的 Automapper 来验证行为更改。使用 automapper 5.1.1 result.MyList 有零个元素。

class TestAutomapper
{
    public static void Test()
    {
        Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<Test, TestDto>();                
        });
        var test = new Test();
        test.MyList.Add(1);

        var result= Mapper.Map<TestDto>(test);
    }
}

public class Test
{
    public List<int> MyList { get; } = new List<int>();
}

public class TestDto
{
    public List<int> MyList { get; } = new List<int>();
}

如何使用 Automapper 5.1.1 使地图正常工作?

4

1 回答 1

2

对于4.2.1 版本,它对我们不起作用。奇怪的是它在4.2.1中为你工作。

为了使它与AutoMapper 4.2.1一起工作,我们必须明确说明如何将原始集合映射到目标集合。

CreateMap.Map<TestDto, Test>()
      .ForMember(x => x.MyList, opt => opt.MapFrom(y => y.MyList));
于 2017-08-24T13:20:03.403 回答