不执行 AutoMapper ForMember 和 MapFrom

AutoMapper ForMember and MapFrom is not executed

ForMember/MapFrom 无论如何都没有执行。

这些是要映射的类;

    public class ImageParams : IEntityParams, IImage, IOperatorFields
{
    public ImageParams()
    {

    }

    public ImageParams(string userId, string title, string description, string imagePath, bool profilePhoto)
    {
        UserId = userId;
        Title = title;
        Description = description;
        ImagePath = imagePath;
        ProfilePhoto = profilePhoto;
    }

    public ImageParams(int id, string userId, string title, string description, string imagePath, bool profilePhoto)
    {
        Id = id;
        UserId = userId;
        Title = title;
        Description = description;
        ImagePath = imagePath;
        ProfilePhoto = profilePhoto;
    }

    [JsonProperty(PropertyName = "id")]
    public int Id { get; set; }
    [JsonProperty(PropertyName = "userId")]
    public string UserId { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    [JsonProperty(PropertyName = "imagePath")]
    public string ImagePath { get; set; }
    [JsonProperty(PropertyName = "profilePhoto")]
    public bool ProfilePhoto { get; set; }
    public Status Status { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime? LastModifiedDate { get; set; }
}
public interface IImage: IEntity, IHasStatus, IDateOperationFields
{
    string UserId { get; set; }
    string Title { get; set; }
    string Description { get; set; }
    string ImagePath { get; set; }
    bool ProfilePhoto { get; set; }
}
public class Image: IImage
{
    public int Id { get; set; }
    public string UserId { get; set; }
    public string Title { get; set; }        
    public string Description { get; set; }
    public string ImagePath { get; set; }
    public bool ProfilePhoto { get; set; }
    public Status Status { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime? LastModifiedDate { get; set; }
    public ApplicationUser ApplicationUser { get; set; }
    public List<ReportImage> Reports { get; set; }
    public List<Report> UserReports { get; set; }
}

我创建的地图如下;

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<Image, ImageParams>().ForMember(x => x.ImagePath, o => o.MapFrom(s => ImagePathFormatting(s.ImagePath))).ReverseMap();
    }

    private static string ImagePathFormatting(string imagePath)
    {
        var formattedImagePath = imagePath.Contains(AppSettingsProvider.PictureBaseUrl) ? imagePath : $"{AppSettingsProvider.PictureBaseUrl}/{imagePath}";
        return formattedImagePath;
    }
 }

我按照以下方式注册我的个人资料;

services.AddAutoMapper(typeof(MappingProfile));

我尝试如下映射;

public ImageParams GetProfileImage(string userId)
    {
        var image = Entities.FirstOrDefault(x => x.UserId == userId && x.ProfilePhoto && x.Status == Status.Active);
        
        return _mapper.Map<ImageParams>(image);
    }

我确定 MappingProfile 已成功执行并将 Image 对象映射到 ImageParams 对象,但是未调用 ImagePathFormatting 函数。

我尝试了很多变体,比如我使用了匿名函数而不是 ImagePathFormatting。我也试过如下使用 IValueResolver;

    public class CustomResolver : IValueResolver<ImageParams, Image, string>
{
    public string Resolve(ImageParams source, Image destination, string imagePath, ResolutionContext context)
    {
        return source.ImagePath.Contains(AppSettingsProvider.PictureBaseUrl) ? source.ImagePath : $"{AppSettingsProvider.PictureBaseUrl}/{source.ImagePath}";
    }
}

无论我尝试什么,我都无法调用 MapFrom 或 CustomResolver。 任何帮助将不胜感激。

第一个解决方案: 从 class ImageParams 中删除除默认无参数之外的任何构造函数:

public class ImageParams : IImage
    {
        public ImageParams()
        {

        }
    //Other members.
    }

第二种方案: 添加 DisableConstructorMapping():

var config = new MapperConfiguration(cfg => {
                    cfg.AddProfile(new MappingProfile());
                    cfg.DisableConstructorMapping();
                }
            );
var mapper = config.CreateMapper();

第三个解决方案:

CreateMap<ImageParams, Image>()
                .ForMember(x => x.ImagePath, o => o.MapFrom(s => ImagePathFormatting(s.ImagePath)))
                .ReverseMap()
                .ForMember(x => x.ImagePath, o => o.MapFrom(s => ImagePathFormatting(s.ImagePath)))
                .ConstructUsing(x => new ImageParams());

Source 1 Source 2

我强烈建议您使用 AutoMapper Execution Plan Tool 工具来查看 automapper 在运行映射时究竟在做什么。

有信心解决您的问题。