简体   繁体   English

使用 AutoMapper.ProjectTo() 方法的 IMongoQueryable 抛出错误

[英]IMongoQueryable throwing error with AutoMapper .ProjectTo() method

The code below gives this error.下面的代码给出了这个错误。 I recently switched to MongoDb instead of Ef Core.我最近改用 MongoDb 而不是 Ef Core。 It seems like automapper doesn't get the object inside it but a serialized form "{document}" or anything else.似乎 automapper 没有在其中获取 object 而是序列化形式“{document}”或其他任何内容。 Can't find much about this and also debugged the automapper code but cannot seem to figure out why this happens.找不到太多关于此的信息,也调试了自动映射器代码,但似乎无法弄清楚为什么会发生这种情况。

Error:错误:

GetPartyFullName of type Application.Read.Common.Helpers.MapHelpers is not supported in the expression tree GetPartyFullName({document}).表达式树 GetPartyFullName({document}) 不支持 Application.Read.Common.Helpers.MapHelpers 类型的 GetPartyFullName。

var partiesList = await parties
            .OrderByDescending(x => x.Created)
            .ThenBy(x => x.Name) // this is IOrderedMongoQueryable
            //.AsQueryable() => not working either
            .ProjectTo<PartyDto>(_mapper.ConfigurationProvider)
            .PaginatedListAsync(request.PageNumber, request.PageSize);

The mapping映射

... code
profile.CreateMap<Party, PartyDto>()
            .ForMember(d => d.FullName, opt => opt.MapFrom(s => MapHelpers.GetPartyFullName(s)));
... more code

Any help would be appreciated.任何帮助,将不胜感激。

As you are using .ProjectTo extension automapper will translate it to select operation.当您使用.ProjectTo扩展名时,自动映射器会将其转换为select操作。

According to docs: The.ProjectTo<OrderLineDTO>() will tell AutoMapper's mapping engine to emit a select clause to the IQueryable... .根据文档: The.ProjectTo<OrderLineDTO>() will tell AutoMapper's mapping engine to emit a select clause to the IQueryable...

So most likely mongo db query translator just does not support your MapHelpers.GetPartyFullName function.所以很可能 mongo db 查询翻译器不支持您的MapHelpers.GetPartyFullName function。 Try to rewrite your mapping directly in the .MapFrom(...) call.尝试直接在.MapFrom(...)调用中重写您的映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM