简体   繁体   English

无法映射 byte[] 或 byte[]? (nullable byte[]) 使用 AutoMapper ForCtorParam 函数

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

Unable to map byte[] or byte[]?无法映射 byte[] 或 byte[]? (nullable byte[]) using AutoMapper ForCtorParam function, I am getting below error message (nullable byte[]) 使用 AutoMapper ForCtorParam 函数,我收到以下错误消息

AutoMapper.AutoMapperConfigurationException : Unmapped members were found. AutoMapper.AutoMapperConfigurationException :找到未映射的成员。 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添加自定义映射表达式,忽略,添加自定义解析器,或修改源/目标类型对于没有匹配的构造函数,添加无参数构造函数,添加可选参数,或映射所有构造函数参数 ======== ================================================== ======================== 模型 -> ModelDto(目标成员列表) AutoMapperForCtorParamTest+Model -> AutoMapperForCtorParamTest+ModelDto(目标成员列表) 未映射的属性:代码链接 ID

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我正在使用启用了可为空类型的 dotnet core 3.1

  2. I don't want to use construct using or convert using functions我不想使用构造 using 或使用函数进行转换

  3. I am using Automapper v 10.0.0我正在使用 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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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