如何映射针对的DTO的OData查询到EF实体? [英] How do I map an OData query against a DTO to an EF entity?
问题描述
我有一个Asp.Net的Web API应用程序,允许的OData查询的ODataController。我只允许读取,不能更新。而不是直接暴露数据模型,我创建了一个集的DTO的。上的DTO的属性名称不完全一致的EF模型的属性。这会导致一个问题,当我尝试使用OData的查询对EF模型。我看着计算器上的其他职位解决此问题,但他们都不似乎解决了这个问题。
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.
下面是我现在所拥有的:
Here is what I have right now:
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
姓氏。这是因为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?
推荐答案
在的OData的WebAPI新功能型号别名可能会解决你的问题。你不必有一个EDM模型和aDTO之间相同的名称。例如,有一个属性名称OrderDto.Total,但在EDM模型变得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();
请参考ODataModelAliasingSample在<一个href=\"https://aspnet.$c$cplex.com/SourceControl/latest#Samples/WebApi/OData/v4/\">https://aspnet.$c$cplex.com/SourceControl/latest#Samples/WebApi/OData/v4/
Please reference the ODataModelAliasingSample in https://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/
这篇关于如何映射针对的DTO的OData查询到EF实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!