如何将OData查询与DTO映射到EF实体? [英] How do I map an OData query against a DTO to an EF entity?

查看:93
本文介绍了如何将OData查询与DTO映射到EF实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个Asp.Net Web Api应用程序中有一个ODataController,允许OData查询。我只允许读取,而不是更新。我不是直接暴露数据模型,而是创建了一组DTO。 DTO上的属性名称不一定与EF模型上的属性相匹配。当我尝试对EF模型使用OData查询时,这会导致问题。我已经看过StackOverflow上的其他帖子,但是没有一个似乎解决了这个问题。

I have an ODataController in an Asp.Net Web Api application that allows OData queries. I'm only allowing reads, not updates. Instead of exposing the data model directly, I've created a set of DTOs. The property names on the DTOs do not necessarily match the properties on the EF model. This causes a problem when I try to use the OData query against the EF model. I've looked at other posts on StackOverflow around this subject but none of them seemed to resolve this issue.

这是我现在所在:

public IQueryable<Customer> GetCustomer(ODataQueryOptions<Customer> query)
{
        ODataModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<Customer>("Customers");
        builder.EntitySet<RECORD>("Records");
        builder.Namespace = "MyDataService.Models";

        var opts = new ODataQueryOptions<RECORD>(new ODataQueryContext(builder.GetEdmModel(), typeof(RECORD)), this.ActionContext.Request);
        var records = (IQueryable<RECORD>)opts.ApplyTo(db.RECORDS);
        return ConvertToCustomerList(records);
}

直到引用选择或过滤器中的特定字段为止。一旦我引用了我的OData查询中的一个字段,我会收到一个类似于的ODataException类型'MyDataService.Models.RECORD '上找不到名为LastName的属性。这是因为EF属性具有不同的命名约定。在这种情况下,它应该使用LAST_NAME。

This works until I reference specific fields in a select or filter. As soon as I reference a field in my OData query I get an ODataException like - Could not find a property named 'LastName' on type 'MyDataService.Models.RECORD. This is because the EF properties have a different naming convention. In this case it should be using "LAST_NAME".

似乎我需要解析查询,然后用正确的名称替换字段引用。我发现ODataUriParser似乎可以帮助这个,但它不像我所希望的那么干净。

It seems like I need to parse the query and then replace the field references with the correct names. I found the ODataUriParser which seems like it could help with this but it's not as clean as I was hoping.

任何人都可以提供一些解决这个问题的指针?有没有更好的方法?

Can anyone provide me some pointers in resolving this issue? Is there a better approach?

推荐答案

WebApi OData新功能模型别名可能会解决您的问题。您不必在Edm Model和aDTO之间拥有相同的名称。例如,有一个属性名称OrderDto.Total,但在Edm Model中,它成为Order.Check

The WebApi OData new feature Model Aliasing may resolve your problem. You don't have to have the same names between a Edm Model and a aDTO. for example, there is a property name OrderDto.Total, but in Edm Model it becomes Order.Check

        ODataModelBuilder builder = new ODataConventionModelBuilder();
        builder.ModelAliasingEnabled = true;

        EntitySetConfiguration<CustomerDto> customers = builder.EntitySet<CustomerDto>("Customers");
        EntitySetConfiguration<OrderDto> orders = builder.EntitySet<OrderDto>("Orders");
        orders.EntityType.Name = "Order";
        orders.EntityType.Property(p => p.Total).Name = "Check";
        return builder.GetEdmModel();

请参考 https://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/

这篇关于如何将OData查询与DTO映射到EF实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆