[英]Automapper Expression Mapping Issue
Expression mapping using AutoMapper.Extensions.Expression
is not mapping the expression.使用
AutoMapper.Extensions.Expression
表达式映射不映射表达式。
Here are my models:这是我的模型:
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
}
My mappers:我的映射器:
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();
}
}
Add a snippet to reproduce the issue:添加一个片段来重现问题:
static void Main(string[] args)
{
MapperConfiguration.Configure();
var email = "test@test.com";
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();
}
The output of this is:这个的输出是:
Old Expression : (x.User.Email == "asd")
旧表达式:(x.User.Email ==“asd”)
New Expression : (x.User.Email == "asd")新表达式:(x.User.Email == "asd")
However, I'm expecting it to output:但是,我希望它输出:
Old Expression : (x.User.Email == "asd")
旧表达式:(x.User.Email ==“asd”)
New Expression : (x.Email == "asd")新表达式:(x.Email == "asd")
The particular lines that aren't working as I expect are:无法按我预期工作的特定行是:
Expression<Func<UserDto, bool>> filterExpression = x => x.User.Email == email;
var expression = Mapper.Map<Expression<Func<UserEntity, bool>>>(filterExpression);
I'm using AutoMapper
v8.0.0 and AutoMapper.Extensions.Expression
v2.0.0我正在使用
AutoMapper
v8.0.0 和AutoMapper.Extensions.Expression
v2.0.0
You forgot to configure the Expression mapping.您忘记配置表达式映射。 Your setup should be:
您的设置应该是:
Mapper.Initialize(cfg =>
{
cfg.AddExpressionMapping();
cfg.AddProfile(new UserDtoProfile());
});
I haven't really used AutoMapper, and have never used the expression mapping, but as per this github issue , the mapping appears to be backwards for expressions.我还没有真正使用过 AutoMapper,也从未使用过表达式映射,但是根据这个 github 问题,表达式的映射似乎是向后的。 Your current mapping won't work, and will crash when executing.
您当前的映射将不起作用,并且在执行时会崩溃。 You'll need to change the mapping to be:
您需要将映射更改为:
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();
}
}
Running this gives the output:运行这个给出输出:
Old Expression : (x.User.Email == "asd")
旧表达式:(x.User.Email ==“asd”)
New Expression : (x.Email == "asd")新表达式:(x.Email == "asd")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.