System.Reflection.TargetInvocationException:配置 AutoMapper 配置文件时,“表达式 'dest => dest' 必须解析为顶级成员”

问题描述 投票:0回答:1

我在 ASP.NET Core 应用程序中尝试配置 AutoMapper 时遇到异常。异常消息是:

System.Reflection.TargetInitationException:调用目标已抛出异常。 ---> System.ArgumentException:表达式“dest => dest”必须解析为顶级成员,而不是任何子对象的属性。您可以使用 ForPath(子类型上的自定义解析器)或 AfterMap 选项来代替。 (参数“lambda表达式”) 在 AutoMapper.Internal.ReflectionHelper.FindProperty(LambdaExpression lambdaExpression)

这是我的 AutoMapper 配置文件配置:

public class AuthenticationMappingProfile : Profile
    {
        public AuthenticationMappingProfile()
        {
            CreateMap<(RegisterRequest registerRequest, string BaseUrl, byte[] Image), RegisterCommand>()
                .ForPath(dest => dest.BaseUrl, opt => opt.MapFrom(src => src.BaseUrl))
                .ForPath(dest => dest.Avatar, opt => opt.MapFrom(src => src.Image))
                .ForPath(dest => dest.FirstName, opt => opt.MapFrom(src => src.registerRequest.FirstName))
                .ForPath(dest => dest.LastName, opt => opt.MapFrom(src => src.registerRequest.LastName))
                .ForPath(dest => dest.Email, opt => opt.MapFrom(src => src.registerRequest.Email))
                .ForPath(dest => dest.Password, opt => opt.MapFrom(src => src.registerRequest.Password))
                .ForPath(dest => dest.ConfirmPassword, opt => opt.MapFrom(src => src.registerRequest.ConfirmPassword))
                .ForPath(dest => dest.Birthday, opt => opt.MapFrom(src => src.registerRequest.Birthday))
                .ForPath(dest => dest.Gender, opt => opt.MapFrom(src => src.registerRequest.Gender))
                .ForPath(dest => dest.Avatar, opt => opt.MapFrom(src => src.Image));
        }
    }

以下是相关类:

 public record RegisterRequest
    {
        public string FirstName { get; init; }
        public string LastName { get; init; }
        public string Email { get; init; }
        public string Password { get; init; }
        public string ConfirmPassword { get; init; }
        public DateTime Birthday { get; init; }
        public string Gender { get; init; }
        public IFormFile? Avatar { get; init; }
    }

    public record RegisterCommand(
        string FirstName,
        string LastName,
        string Email,
        string Password,
        string ConfirmPassword,
        DateTime Birthday,
        string Gender,
        byte[]? Avatar,
        string BaseUrl
    ) : IRequest<ErrorOr<AuthenticationResult>>;

在AutoMapper配置文件创建过程中抛出异常。目标是将元组 (RegisterRequest registerRequest, string BaseUrl, byte[] Image) 映射到 RegisterCommand

我尝试过的:

验证目标类和源类中的属性名称和类型。 检查了用于映射复杂类型的 AutoMapper 文档并使用了 .ForPath 和 .ForMember

c# asp.net entity-framework asp.net-web-api automapper
1个回答
0
投票

使用

ForCtorParam

CreateMap<(RegisterRequest registerRequest, string BaseUrl, byte[] Image), RegisterCommand>()
    .ForCtorParam("BaseUrl", opt => opt.MapFrom(src => src.BaseUrl))
    .ForCtorParam("Avatar", opt => opt.MapFrom(src => src.Image))
    .ForCtorParam("FirstName", opt => opt.MapFrom(src => src.registerRequest.FirstName))
    .ForCtorParam("LastName", opt => opt.MapFrom(src => src.registerRequest.LastName))
    .ForCtorParam("Email", opt => opt.MapFrom(src => src.registerRequest.Email))
    .ForCtorParam("Password", opt => opt.MapFrom(src => src.registerRequest.Password))
    .ForCtorParam("ConfirmPassword", opt => opt.MapFrom(src => src.registerRequest.ConfirmPassword))
    .ForCtorParam("Birthday", opt => opt.MapFrom(src => src.registerRequest.Birthday))
    .ForCtorParam("Gender", opt => opt.MapFrom(src => src.registerRequest.Gender))
    .ForCtorParam("Avatar", opt => opt.MapFrom(src => src.Image));
© www.soinside.com 2019 - 2024. All rights reserved.