Daniel
Daniel

Reputation: 9839

Odata Configuration for Mapping from ReadModel to Entity

I am using Microsoft.AspNetCore.OData 7.3.0.

My entity class:

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
}

I want to expose a 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; }
}

The configuration for StandardProjectReportReadModel currently looks like:

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);
        }
    }
}

My 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();
    }
}

When i call: http://localhost:5103/odata/StandardProjectReport?api-version=1.0 i get a 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()

My question: How to configure a mapping between ReadModel and Entity?

Upvotes: 3

Views: 433

Answers (2)

Lohith Kumar
Lohith Kumar

Reputation: 1

Keep the same Model name ControllingProjectReportReadModel in the GetEdmModel.
Example :

var builder = new ODataConventionModelBuilder();
builder.EntitySet<ControllingProjectReportReadModel>("ReportReadModel");

Upvotes: 0

Daniel
Daniel

Reputation: 9839

Looks like the controller was causing the error.

I needed to passODataQueryOptions<ControllingProjectReportReadModel> odataQuery instead of ODataQueryOptions<ProjectReport> odataQuery.

The controller method now looks like:

[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();
}

Upvotes: 1

Related Questions