我不明白为什么这不起作用?两个字段在destinationDTO上映射为NULL。 使用AutoMapper v4.1.1
[TestFixture]
public class AutoMapperTests
{
[SetUp]
public void Init()
{
AutoMapperTestConfiguration.Configure();
}
[Test]
public void Mapping_With_Underscores()
{
SourceDTO source = new SourceDTO
{
first_name = "John",
last_name = "Doe"
};
var result = Mapper.Map<DestinationDTO>(source);
Assert.That(result.FirstName == "John");
}
}
public class SourceDTO
{
public string first_name { get; set; }
public string last_name { get; set; }
}
public class DestinationDTO
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class AutoMapperTestConfiguration
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.AddProfile<FromUnderscoreMapping>();
});
}
}
public class FromUnderscoreMapping : Profile
{
protected override void Configure()
{
this.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
this.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
Mapper.CreateMap<SourceDTO, DestinationDTO>();
}
}
答案 0 :(得分:1)
Mapper.CreateMap<SourceDTO, DestinationDTO>();
我正在调用全局AutoMapper实例,而不是具有覆盖命名约定的继承Profile。 只需删除Mapper就可以解决问题。
CreateMap<SourceDTO, DestinationDTO>();
我在这个上花了很多时间。