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";
}
}
How AutoMapper works? AutoMapper internally uses a great concept of programming called Reflection. Reflection in C# is used to retrieve metadata on types at runtime. With the help of Reflection, we can dynamically get a type of existing objects and invoke its methods or access its fields and properties.
The . ProjectTo<OrderLineDTO>() will tell AutoMapper's mapping engine to emit a select clause to the IQueryable that will inform entity framework that it only needs to query the Name column of the Item table, same as if you manually projected your IQueryable to an OrderLineDTO with a Select clause.
AutoMapper supports polymorphic arrays and collections, such that derived source/destination types are used if found.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With