【问题标题】:Does automapper with odata support multiple orderby /?$orderby=Field1,Field2带有 odata 的 automapper 是否支持多个 orderby /?$orderby=Field1,Field2
【发布时间】:2020-05-14 00:56:32
【问题描述】:

.net 核心 2.2、automapper 9.0.0、efcore 2.2.6、odata 7.2.3 为了在上下文中使用自动映射,我使用 AutoMapper.AspNetCore.OData.EFCore" Version="1.0.0" 包

public class RolesController : ODataController
{
    private readonly ApplicationDbContext _context;
    private readonly IMapper _mapper;

    public RolesController(ApplicationDbContext context, IMapper mapper)
    {
        _context = context ?? throw new ArgumentNullException(nameof(context));
        _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
    }

    [EnableQuery]
    public async Task<IActionResult> Get(ODataQueryOptions<RoleGridRow> options)
    {
        return Ok(await _context.Roles.AsNoTracking().GetQueryAsync(_mapper, options));
    }
}


public RolesProfile()
    {
        CreateMap<ApplicationRole, RoleGridRow>()
            .ForMember(dest => dest.Users, opt => opt.MapFrom(src => src.UserRoles ));
        CreateMap<ApplicationUserRole, User>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.UserId))
            .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.User.Name));
    }

ApplicationRole/User/UserRole 继承自 IdentityRole/User/UserRole 所有导航设置正确。

我需要以下 DTO

 public class RoleGridRow
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public bool IsActive { get; set; }
    public List<User> Users { get; set; }
}

使用 OData,所有调用似乎都在工作:orderby、扩展到用户、选择、...

对于 orderby,当我尝试按多个字段排序时,例如:“https://localhost:5001/odata/roles?$orderby=IsActive,Name”,我收到以下异常:

No generic method 'ThenBy' on type 'System.Linq.Queryable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.

automapper 是否支持这一点?还是我忽略了什么?

【问题讨论】:

标签: odata automapper ef-core-2.2


【解决方案1】:

包中有错误。找到了,修好了。我将创建一个 PR。发现了一些关于分页/计数的其他错误。

已报告,也会为此创建 PR。 现在也需要移除 EnableQuery。

【讨论】:

    猜你喜欢
    • 2015-07-04
    • 1970-01-01
    • 2017-02-18
    • 1970-01-01
    • 2012-05-27
    • 2022-01-05
    • 2013-12-10
    • 2016-02-27
    • 1970-01-01
    相关资源
    最近更新 更多