Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapper ResolveUsing cause "Can't resolve this to Queryable Expression"

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";
    }

}
like image 573
javirnof Avatar asked May 12 '15 07:05

javirnof


People also ask

How does AutoMapper work internally?

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.

What is ProjectTo AutoMapper?

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.

Can AutoMapper map collections?

AutoMapper supports polymorphic arrays and collections, such that derived source/destination types are used if found.


1 Answers

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.

like image 160
Jimmy Bogard Avatar answered Oct 21 '22 20:10

Jimmy Bogard