我正在使用 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);
}
}
}
我的 Controller :
[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我得到一个异常(exception):
System.ArgumentException: The given model does not contain the type 'Reporting.Core.Models.ProjectReport'. (Parameter 'elementClrType') at Microsoft.AspNet.OData.ODataQueryContext..ctor(IEdmModel model, Type elementClrType, ODataPath path) at Microsoft.AspNet.OData.ODataQueryParameterBindingAttribute.ODataQueryParameterBinding.BindModelAsync(ModelBindingContext bindingContext) at 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 之间的映射?
最佳答案
看起来是 Controller 导致了错误。
我需要通过ODataQueryOptions<ControllingProjectReportReadModel> odataQuery
而不是ODataQueryOptions<ProjectReport> odataQuery
.
Controller 方法现在看起来像:
[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();
}
关于c# - 用于从 ReadModel 映射到实体的 Odata 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60395846/