C# 用于从ReadModel映射到实体的Odata配置

C# 用于从ReadModel映射到实体的Odata配置,c#,odata,C#,Odata,我使用的是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 {

我使用的是
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

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);
        }
    }
}
[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();
}
公共类StandardProjectReportModelConfiguration:IModelConfiguration
{
私有静态只读ApiVersion V1=新ApiVersion(1,0);
私有实体类型配置当前(ODataModelBuilder生成器)
{
var order=builder.EntitySet(“StandardProjectReport”).EntityType;
order.HasKey(p=>p.OptionId);
退货单;
}
公共无效应用(ODataModelBuilder builder,ApiVersion)
{
//注:订单的EDM仅在版本1.0中可用
如果(apiVersion==V1)
{
配置当前(生成器);
}
}
}
我的控制器:

[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();
    }
}
[授权]
[ApiVersion(“1.0”)]
[ODataRoutePrefix(“标准项目报告”)]
[ApiExplorerSettings(IgnoreApi=false)]
公共类StandardProjectReportController:ODataController
{
私有只读IReportingReadOnlyContext\u readContext;
私有只读IIdentityService _identityService;
专用只读IMapper\u映射器;
公共标准项目报告控制器(IReportingReadOnlyContext readContext、IIdentityService identityService、IMapper映射器)
{
_readContext=readContext;
_identityService=identityService;
_映射器=映射器;
}
[HttpGet]
[ODataRoute]
[启用查询(页面大小=300)]
公共IQueryable获取(ODataQueryOptions odataQuery)
{
var userId=“MyId;
//当我们处理实体并将其投影回模型时,应用过滤器
var executedQuery=\u readContext.GetProjectReportsFilteredByCwsId(userId).Get(\u mapper,odataQuery);
返回_mapper.Map(executedQuery).AsQueryable();
}
}
当我呼叫时:我得到一个异常:

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绑定上下文) 位于Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext ActionContext、IModelBinder modelBinder、IValueProvider valueProvider、ParameterDescriptor参数、ModelMetadata元数据、对象值) 在Microsoft.AspNetCore.Mvc.Controllers.ControllerBinderDelegateProvider.c_uuDisplayClass0_0.d.MoveNext()中


我的问题:如何配置ReadModel和Entity之间的映射?

看起来是控制器导致了错误

我需要传递
ODataQueryOptions-odataQuery
而不是
ODataQueryOptions-odataQuery

控制器方法现在看起来像:

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);
        }
    }
}
[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();
}
[HttpGet]
[ODataRoute]
[启用查询(页面大小=300)]
公共IQueryable获取(ODataQueryOptions odataQuery)
{
//当我们处理实体并将其投影回模型时,应用过滤器
var executedQuery=\u readContext.ProjectReport.Get(\u mapper,odataQuery);
返回_mapper.Map(executedQuery).AsQueryable();
}