I'm getting the following error in AutoMapper when trying to convert an object with a property that is of type byte[] to an object with a matching property of type string:
System.InvalidOperationException: Missing map from System.Byte to System.Char. Create using CreateMap<Byte, Char>.
I tried using custom type converters, but they didn't seem to work (same error with or without them). I was able to map the specific property, but I'm trying to create something that can be applied to an entire project (most of my entities include a RowVersion that is intended to be used for optimistic locking).
My code look something like this...
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 Convert(byte[] source, string destination, ResolutionContext context)
{
return System.Convert.ToBase64String(source);
}
}
public class StringToByteArrayTypeConverter : ITypeConverter<string, byte[]>
{
public byte[] Convert(string source, byte[] destination, ResolutionContext context)
{
return System.Convert.FromBase64String(source);
}
}
This is in a .Net 5, ASP.Net Core, Web API project.