Конфигурация Odata для преобразования из ReadModel в Entity - PullRequest
3 голосов
/ 25 февраля 2020

Я использую Microsoft.AspNetCore.OData 7.3.0.

Мой класс сущности:

public class ProjectReport
{
    public int OptionId { get; set; }
    public int Hash { get; set; }
    public int ProjectNo { get; set; }
    public int RevisionNo { get; set; }
    public int OptionNo { get; set; }
    public string CreatedBy { get; set; }
    // many more
}

Я хочу выставить Модель чтения

public class StandardProjectReportReadModel
{
    public int OptionId { get; set; }
    public int Hash { get; set; }
    public int ProjectNo { get; set; }
    public int RevisionNo { get; set; }
    public int OptionNo { get; set; }
    public string CreatedBy { get; set; }
}

Конфигурация для StandardProjectReportReadModel в настоящее время выглядит следующим образом:

public class StandardProjectReportModelConfiguration : IModelConfiguration
{
    private static readonly ApiVersion V1 = new ApiVersion(1, 0);

    private EntityTypeConfiguration<StandardProjectReportReadModel> ConfigureCurrent(ODataModelBuilder builder)
    {
        var order = builder.EntitySet<StandardProjectReportReadModel>("StandardProjectReport").EntityType;

        order.HasKey(p => p.OptionId);

        return order;
    }

    public void Apply(ODataModelBuilder builder, ApiVersion apiVersion)
    {
        // note: the EDM for orders is only available in version 1.0
        if (apiVersion == V1)
        {
            ConfigureCurrent(builder);
        }
    }
}

Мой контроллер:

[Authorize]
[ApiVersion("1.0")]
[ODataRoutePrefix("StandardProjectReport")]
[ApiExplorerSettings(IgnoreApi = false)]
public class StandardProjectReportController : ODataController
{
    private readonly IReportingReadOnlyContext _readContext;
    private readonly IIdentityService _identityService;
    private readonly IMapper _mapper;

    public StandardProjectReportController(IReportingReadOnlyContext readContext, IIdentityService identityService, IMapper mapper)
    {
        _readContext = readContext;
        _identityService = identityService;
        _mapper = mapper;
    }

    [HttpGet]
    [ODataRoute]
    [EnableQuery(PageSize = 300)]
    public IQueryable<StandardProjectReportReadModel> Get(ODataQueryOptions<ProjectReport> odataQuery)
    {
        var userId = "MyId;

        // Apply the filter as we are working on the Entity and project back to a model
        var executedQuery = _readContext.GetProjectReportsFilteredByCwsId(userId).Get(_mapper, odataQuery);

        return _mapper.Map<IList<StandardProjectReportReadModel>>(executedQuery).AsQueryable();
    }
}

Когда я звоню: http://localhost: 5103 / odata / StandardProjectReport? Api-version = 1.0 Я получаю исключение:

System.ArgumentException: Данная модель не содержит тип 'Reporting.Core.Models.ProjectReport'. (Параметр 'elementClrType') в Microsoft.As pNet .OData.ODataQueryContext..ctor (модель IEdmModel, тип elementClrType, путь ODataPath) в Microsoft.As pNet .OData.ODataQueryParameterBindingAttribute.ODataQueryParaodelindingTextBindMindConnectBindingMind. bindingContext) в Microsoft.AspNetCore. Mvc .ModelBinding.Binders.Binder.By.TimepeModelBinder.BindModelAsyn c (ModelBindingContext bindingContext) в Microsoft.AspNetCore. Mvc .ModelBinding.ParameterBinder. valueProvider, параметр ParameterDescriptor, метаданные ModelMetadata, значение объекта) в Microsoft.AspNetCore. Mvc .Controllers.ControllerBinderDelegateProvider. <> c__DisplayClass0_0. d.MoveNext ()

Мой вопрос: Как настроить отображение между ReadModel и Entity?

1 Ответ

1 голос
/ 26 февраля 2020

Похоже, что контроллер вызвал ошибку.

Мне нужно было передать ODataQueryOptions<ControllingProjectReportReadModel> odataQuery вместо ODataQueryOptions<ProjectReport> odataQuery.

Метод контроллера теперь выглядит следующим образом:

[HttpGet]
[ODataRoute]
[EnableQuery(PageSize = 300)]
public IQueryable<ControllingProjectReportReadModel> Get(ODataQueryOptions<ControllingProjectReportReadModel> odataQuery)
{
    // Apply the filter as we are working on the Entity and project back to a model
    var executedQuery = _readContext.ProjectReport.Get(_mapper, odataQuery);

    return _mapper.Map<IList<ControllingProjectReportReadModel>>(executedQuery).AsQueryable();
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...