当尝试将具有 byte[] 类型属性的对象转换为具有字符串类型匹配属性的对象时,我在 AutoMapper 中收到以下错误:
System.InvalidOperationException:缺少从 System.Byte 到 System.Char 的映射。使用 CreateMap<Byte, Char> 创建。
我尝试使用自定义类型转换器,但它们似乎不起作用(无论有或没有它们都会出现相同的错误)。我能够映射特定的属性,但我正在尝试创建可以应用于整个项目的东西(我的大多数实体都包含旨在用于乐观锁定的 RowVersion)。
我的代码看起来像这样......
public class AutoMapperProfile : AutoMapper.Profile
{
public AutoMapperProfile()
{
CreateMap<byte[], string>().ConvertUsing<ByteArrayToStringTypeConverter>();
CreateMap<string, byte[]>().ConvertUsing<StringToByteArrayTypeConverter>();
CreateMap<MyFirstClass, MySecondClass>();
}
}
public class MyFirstClass
{
public string Name { get; set; }
public byte[] RowVersion { get; set; }
}
public class MySecondClass
{
public string Name { get; set; }
public string RowVersion { get; set; }
}
public class ByteArrayToStringTypeConverter : ITypeConverter<byte[], string>
{
public string …Run Code Online (Sandbox Code Playgroud)