Reputation: 49
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 For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters ================================================================================== Model -> ModelDto (Destination member list) AutoMapperForCtorParamTest+Model -> AutoMapperForCtorParamTest+ModelDto (Destination member list) Unmapped properties: Code LinkId
Sample code to recreate below
public class AutoMapperForCtorParamTest
{
private readonly IMapper mapper;
public AutoMapperForCtorParamTest()
{
mapper = new Mapper(new MapperConfiguration(
cfg =>
{
cfg.AddProfile<ModelMapperProfile>();
}));
}
[Fact]
public void MapAllProperties()
{
mapper.ConfigurationProvider.AssertConfigurationIsValid();
}
public class ModelMapperProfile : Profile
{
public ModelMapperProfile()
{
var defaultCode = Guid.NewGuid().ToByteArray();
CreateMap<Model, ModelDto>()
.ForCtorParam("type", cfg => cfg.MapFrom(x => 1))
.ForCtorParam("code", cfg => cfg.MapFrom<byte[]>(x => defaultCode))
.ForCtorParam("linkId", cfg => cfg.MapFrom<byte[]?>(x => null));
}
}
public class ModelDto
{
public ModelDto(int id, string name, int type, byte[] code, byte[]? linkId)
{
Id = id;
Name = name;
Type = type;
Code = code;
LinkId = linkId;
}
public int Id { get; }
public string Name { get; }
public int Type { get; }
public byte[] Code { get; }
public byte[]? LinkId { get; }
}
public class Model
{
public Model(int id, string name)
{
Id = id;
Name = name;
}
public int Id { get; }
public string Name { get; }
}
}
Note:
I am using dotnet core 3.1 with nullable types enabled
I don't want to use construct using or convert using functions
I am using Automapper v 10.0.0
Upvotes: 0
Views: 421
Reputation: 8955
Use:
.ForCtorParam("text", opt => opt.MapFrom<byte[]?>(src => null));
In my case (string) I fixed it with:
.ForCtorParam("text", opt => opt.MapFrom<string>(src => null));
Upvotes: 0