【发布时间】: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 是否支持这一点?还是我忽略了什么?
【问题讨论】:
-
在github上打开了一个问题:github.com/AutoMapper/AutoMapper.Extensions.OData/issues/3
标签: odata automapper ef-core-2.2