у меня есть 2 классов я хочу сопоставить с Automapper:Automapper именования со свойствами Подчеркивание/PascalCase
namespace AutoMapperApp
{
public class Class1
{
public string Test { get; set; }
public string property_name { get; set; }
}
}
namespace AutoMapperApp
{
public class Class2
{
public string Test { get; set; }
public string PropertyName { get; set; }
}
}
Это мой Automapper конфигурации:
using AutoMapper;
namespace AutoMapperApp
{
public static class AutoMapperConfig
{
public static MapperConfiguration MapperConfiguration;
public static void RegisterMappings()
{
MapperConfiguration = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Class1, Class2>();
cfg.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
cfg.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
});
}
}
}
Согласно Вики Automapper это должно работать : https://github.com/AutoMapper/AutoMapper/wiki/Configuration
Но мой UnitTest терпит неудачу:
using Xunit;
using AutoMapperApp;
namespace AutoMapperTest
{
public class Test
{
[Fact]
public void AssertConfigurationIsValid()
{
AutoMapperConfig.RegisterMappings();
AutoMapperConfig.MapperConfiguration.AssertConfigurationIsValid();
}
}
}
Исключение:
AutoMapper.AutoMapperConfigurationException:
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
=============================================
Class1 -> Class2 (Destination member list)
AutoMapperApp.Class1 -> AutoMapperApp.Class2 (Destination member list)
Unmapped properties:
PropertyName
Почему?
Mapper.Initialize не рекомендуется, так как версия 4.2: https://lostechies.com/jimmybogard/2016/01/21/removing-the-static-api-from-automapper/ Свойства с точно одно и то же имя отображается без проблем. – PJL