简体   繁体   中英

Unable to map byte[] or byte[]? (nullable byte[]) using AutoMapper ForCtorParam function

Unable to map byte[] or byte[]? (nullable byte[]) using AutoMapper ForCtorParam function, I am getting below error message

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:

  1. I am using dotnet core 3.1 with nullable types enabled

  2. I don't want to use construct using or convert using functions

  3. I am using Automapper v 10.0.0

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM