簡體   English   中英

用於從 ReadModel 映射到實體的 Odata 配置

[英]Odata Configuration for Mapping from ReadModel to Entity

我正在使用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
}

我想公開一個ReadModel

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.AspNet.OData.ODataQueryContext..ctor(IEdmModel 模型,類型 elementClrType,ODataPath 路徑)在 Microsoft.AspNet.OData.ODataQueryParameterBindingAttribute.ODataQueryParameterBinding.BindModelAsync(ModelBindingContext bindingContext)在 Microsoft.AspNetCore.Mvc。 ModelBinding.Binders.BinderTypeModelBinder.BindModelAsync(ModelBindingContext bindingContext) at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext actionContext, IModelBinder modelBinder, IValueProvider valueProvider, ParameterDescriptor parameter, ModelMetadata metadata, Object value) at Microsoft.AspNetCore.Mvc.Controllers .ControllerBinderDelegateProvider.<>c__DisplayClass0_0.<g__Bind|0>d.MoveNext()

我的問題:如何配置 ReadModel 和 Entity 之間的映射?

看起來是控制器導致了錯誤。

我需要傳遞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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM