当我们在 EF Core 中使用 OData 和 Mapster 以及 customDTO 时。 OdataQueryOptions 正在被覆盖

问题描述 投票:0回答:2

我们要做的是承运人附属的租户并不是在所有情况下都来。

我的 .NET Core 项目中有 OData 和 Mapster。我想使用

ProjectToType
将 OData 查询转换为 dto,我也在 Mapster 中使用它。但是,
ProjectToType
会覆盖 OData 查询生成的 EF Core 查询。它通过一个大的 SQL 查询进入数据库。它尝试处理内存中的所有事务,但我只想从数据库获取一个属性。我在下面分享了我的项目的详细信息和我预期的结果。

控制器:

[Area("odata")]
[Route("[area]/v{v:apiVersion}/[controller]")]
public class CargoCarriersODataController : ODataControllerBase
{
    private readonly IGenericReadRepository<Guid> _genericReadRepository;

    public CargoCarriersODataController(IGenericReadRepository<Guid> genericReadRepository)
    {
        _genericReadRepository = genericReadRepository;
    }

    [HttpGet]
    [EnableQuery]
    [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IQueryable<CargoCarriersODataDto>))]
    public async Task<ActionResult<IQueryable<CargoCarriersODataDto>>> Get(ODataQueryOptions<Carrier> opts)
    {
        var carriers = _genericReadRepository.GetAll<Carrier>();
        opts.ApplyTo(carriers);
        return Ok(carriers.ProjectToType<CargoCarriersODataDto>());
    }
}

DTO 课程:

public class CargoCarriersODataDto
{
    public int RId { get; set; }
    public Guid TenantId { get; set; }
    public TenantDto Tenant { get; set; }
    public Guid CargoCarrierId { get; set; }
    public string Name { get; set; }
    public string EnabledLogoUrl { get; set; }
    public string DisabledLogoUrl { get; set; }
    public bool IsEnabled { get; set; }
    public bool DoesSupportSameDayShipment { get; set; }
}

public class TenantDto
{
    public int RId { get; set; }
    public string Name { get; set; }
    public string CompanyName { get; set; }
    public string PhoneNumber { get; set; }
    public string CompanyEmail { get; set; }
}

public class CargoCarriersDtoCustomMap : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        config.ForType<Carrier, CargoCarriersODataDto>()
            .Map(dest => dest.RId, src => src.RId)
            .Map(dest => dest.TenantId, src => src.TenantId)
            .Map(dest => dest.CargoCarrierId, src => src.CargoCarrierId)
            .Map(dest => dest.Name, src => src.Name)
            .Map(dest => dest.EnabledLogoUrl, src => src.EnabledLogoUrl)
            .Map(dest => dest.DisabledLogoUrl, src => src.DisabledLogoUrl)
            .Map(dest => dest.IsEnabled, src => src.IsEnabled)
            .Map(dest => dest.DoesSupportSameDayShipment, src => src.DoesSupportSameDayShipment)
            .Map(dest => dest.Tenant, src => src.Tenant);
    }
}

public class TenantDtoCustomMap : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        config.ForType<Tenant, TenantDto>()
        .Ignore(dest => dest, src => new TenantDto
        {
            RId = src.RId,
            CompanyEmail = src.CompanyEmail,
            Name = src.Name,
            CompanyName = src.CompanyName,
            PhoneNumber = src.PhoneNumber
        });
    }
}

我们发送时期望的查询为

http://localhost:8076/odata/v1/CargoCarriersOData?$select=name
SELECT N'Name' AS [Name], [c].[Name] AS [Value], N'Id' AS [Name], [c].[Id] AS [Value]
FROM [Carriers] AS [c]
WHERE [c].[DeletedAt] IS NULL AND ([c].[TenantId] = 'fa9f75b5-c518-40af-8731-020815551704')

但是OData生成的查询

SELECT [c].[RId], [c].[TenantId], CAST(0 AS bit), [t0].[RId], [t0].[Name], [t0].[CompanyName], [t0].[PhoneNumber], [t0].[CompanyEmail], [c].[CargoCarrierId], [c].[Name], [c].[EnabledLogoUrl], [c].[DisabledLogoUrl], [c].[IsEnabled], [c].[DoesSupportSameDayShipment]
FROM [Carriers] AS [c]
INNER JOIN (
SELECT [t].[Id], [t].[CompanyEmail], [t].[CompanyName], [t].[CreatedAt], [t].[CreatedBy], [t].[DeletedAt], [t].[DeletedBy], [t].[Name], [t].[PhoneNumber], [t].[RId], [t].[StorageAccountConnectionString], [t].[UpdatedAt], [t].[UpdatedBy], [t].[Version]
FROM [Tenants] AS [t]
WHERE [t].[DeletedAt] IS NULL
) AS [t0] ON [c].[TenantId] = [t0].[Id]
WHERE [c].[DeletedAt] IS NULL AND ([c].[TenantId] = 'fa9f75b5-c518-40af-8731-020815551704')

我正在使用

  • .NET Core 3.1
  • OData 7.5.7
  • Mapster 4.1.1
c# entity-framework-core odata mapster
2个回答
0
投票

我看到两个问题:

  1. opts.ApplyTo(..)
    reutrns
    IQueryable
    ,但您的代码忽略结果
  2. ApplyTo
    适用于
    IQueryable<Carrier>
    但应适用于
    IQueryable<CargoCarriersODataDto>
[HttpGet]
[EnableQuery]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IQueryable<CargoCarriersODataDto>))]
public async Task<ActionResult<IQueryable<CargoCarriersODataDto>>> Get(ODataQueryOptions<Carrier> opts)
{
    var carriers = _genericReadRepository.GetAll<Carrier>();
    var projected = carriers.ProjectToType<CargoCarriersODataDto>();
    projected = opts.ApplyTo(projected);
    return Ok(projected);
}

0
投票

如果可能的话,您可以重写为类似的内容。

[HttpGet] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IQueryable))] 公共异步任务> Get(ODataQueryOptions opts) { var sqlQuery = opts.ApplyTo(dbContext.Carriers).ToQueryString(); var 结果 = dbContext.Carriers.FromSqlRaw(query); 返回 Ok(结果.Adapt()); }

© www.soinside.com 2019 - 2024. All rights reserved.