c# - Automapper 表达式映射问题

标签 c# lambda automapper automapper-8

使用 AutoMapper.Extensions.Expression 的表达式映射未映射表达式。

这是我的模型:

public class UserDto
{
    public int Id { get; set; }
    public User User { get; set; }
    //other info
}

public class User
{
    public string Email { get; set; }
    //other info
}


public class UserEntity
{
    public int Id { get; set; }
    public string Email { get; set; }
    //other info
}

我的映射器:

public static class MapperConfiguration
{
    public static void Configure()
    {
        Mapper.Initialize(cfg =>
        {
            cfg.AddProfile(new UserDtoProfile());
        });
    }
}

internal class UserDtoProfile : Profile
{
    public UserDtoProfile()
    {
        CreateMap<UserDto, UserEntity>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
            .ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.User.Email));

        CreateMap<UserEntity, UserDto>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
            .ForMember(dest => dest.User, opt => opt.MapFrom(src => Mapper.Map<UserEntity>(src)));

        CreateMap<UserEntity, User>()
           .ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.Email)).ReverseMap();
    }
}

添加一个代码片段来重现该问题:

static void Main(string[] args)
{
    MapperConfiguration.Configure();
    var email = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85f1e0f6f1c5f1e0f6f1abe6eae8" rel="noreferrer noopener nofollow">[email protected]</a>";
    Expression<Func<UserDto, bool>> filterExpression = x => x.User.Email == email;
    var expression = Mapper.Map<Expression<Func<UserEntity, bool>>>(filterExpression);
    Console.WriteLine("Old Expression : " + filterExpression.Body);
    Console.WriteLine("New Expression : " + expression.Body);
    Console.Read();
}

其输出是:

Old Expression : (x.User.Email == "asd")
New Expression : (x.User.Email == "asd")

但是,我希望它输出:

Old Expression : (x.User.Email == "asd")
New Expression : (x.Email == "asd")

未按我预期工作的特定行是:

Expression<Func<UserDto, bool>> filterExpression = x => x.User.Email == email;
var expression = Mapper.Map<Expression<Func<UserEntity, bool>>>(filterExpression);

我正在使用 AutoMapper v8.0.0 和 AutoMapper.Extensions.Expression v2.0.0

最佳答案

您忘记配置表达式映射。您的设置应该是:

Mapper.Initialize(cfg =>
{
    cfg.AddExpressionMapping();
    cfg.AddProfile(new UserDtoProfile());
});

我还没有真正使用过AutoMapper,也从未使用过表达式映射,但是按照this github issue ,表达式的映射似乎是向后的。您当前的映射将不起作用,并且在执行时会崩溃。您需要将映射更改为:

internal class UserDtoProfile : Profile
{
    public UserDtoProfile()
    {
        CreateMap<UserDto, UserEntity>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
            .ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.User.Email))
            .ReverseMap();

        CreateMap<UserEntity, User>()
           .ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.Email)).ReverseMap();
    }
}

运行此命令会给出输出:

Old Expression : (x.User.Email == "asd")
New Expression : (x.Email == "asd")

关于c# - Automapper 表达式映射问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53940242/

相关文章:

c# - 确保插入发生在多个表上

javascript - 关于在 AWS NodeJS 上使用 TypeScript。 (获取 TS 而不是 JS 的堆栈跟踪)

xml - 使用AutoMapper将特定的XML元素从字符串分配给ViewModel

.net - 如何配置 Automapper 以注入(inject) Ninject 2.0?

entity-framework - 在 AutoMapper 8.0 中缺少 ResolveUsing

c# - 如何创建不同类型的 Expression<Func<T, object>> 的集中提供者?

c# - ASP.NET Web UI ListView 不会刷新

javascript - 如何将日期时间值附加到表单数据并在 Controller 中接收它

c++ - 将捕获的 lambda 作为函数指针传递

c++ - 使 lambda 不可复制/不可移动