问题描述
我使用autommaper映射域类模型类,反之亦然。我需要加密/解密的一个属性。当我映射模型域名没有问题,工作perefectly:
Mapper.CreateMap< EntityModel,实体方式>()ForMember(DEST => dest.Password,选择=> opt.ResolveUsing(SRC => this.EncryptString(SRC 。密码)))
但是,当实体映射到模型automapper崩溃,并引发无法解析这可查询前pression
Mapper.CreateMap<实体,EntityModel方式>()ForMember(DEST => dest.Password,选择=> opt.ResolveUsing(SRC => this.DecryptString(SRC 。密码)))
我和一个自定义值解析器尝试过,用同样的结果:
Mapper.CreateMap<实体,EntityModel>()ForMember(DEST =方式> dest.Password,OP => op.ResolveUsing< PasswordResolver方式>()FromMember(X = > x.Password));
公共类PasswordResolver:ValueResolver<对象,字符串>
{
保护覆盖字符串ResolveCore(对象源)
{
返回TEST;
}}
作为文档状态,你不能使用可查询前pressions定制解析器:
的
您可以,但是,使用MapFrom:
Mapper.CreateMap<实体,EntityModel>()
.ForMember(DEST => dest.Password,OP => op.MapFrom(SRC =>中考));
我猜那并不是你想为Password属性做什么,但是这就是你如何修复的例子。
I'm using autommaper to map domain classes to model classes and viceversa. I need to encrypt/decrypt one property. When I map Model to Domain there isn't problem, work perefectly:
Mapper.CreateMap<EntityModel, Entity>().ForMember(dest => dest.Password, opt => opt.ResolveUsing(src => this.EncryptString(src.Password)))
But when map Entity to Model automapper crash and throws "Can't resolve this to Queryable Expression":
Mapper.CreateMap<Entity, EntityModel>().ForMember(dest => dest.Password, opt => opt.ResolveUsing(src => this.DecryptString(src.Password)))
I've tried with a Custom Value Resolver too, with same result:
Mapper.CreateMap<Entity, EntityModel>().ForMember(dest => dest.Password, op => op.ResolveUsing<PasswordResolver>().FromMember(x => x.Password));
public class PasswordResolver : ValueResolver<object, string>
{
protected override string ResolveCore(object source)
{
return "TEST";
}
}
As the documentation states, you can't use custom resolvers in queryable expressions:
https://github.com/AutoMapper/AutoMapper/wiki/Queryable-Extensions#supported-mapping-options
You can, however, use MapFrom:
Mapper.CreateMap<Entity, EntityModel>()
.ForMember(dest => dest.Password, op => op.MapFrom(src => "TEST"));
I'm guessing that's not actually what you want to do for that Password property but that's how you can fix the example.
这篇关于Automapper ResolveUsing原因和QUOT;无法解析这可查询前pression&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!