OData entity property serialization name different than C# variable name

萝らか妹 提交于 2019-12-22 05:23:35

问题


In an ASP .NET Web API using OData, I have a C# object describing the fields allowed to be used in $filter

Say I want to limit $filter support to only $filter=deviceId gt 'someValue' (followed http://www.ben-morris.com/parsing-odata-queries-decoupled-data-entities-webapi/ approach...).

So I defined a class like this:

[DataContract]
public class DeviceODataQuery
{
    [DataMember(Name = "deviceId")]
    public string DeviceId { get; set; }
}

Then I build myself the OData context because I can't use IQueryable (too long to explain why, its legacy code...).

It looks like this:

/* Configure supported fields for query */
var builder = new ODataConventionModelBuilder();
builder.EntitySet<DeviceODataQuery>("DeviceODataQuery");
builder.Namespace = typeof(DeviceODataQuery).Namespace;
var model = builder.GetEdmModel();
ODataQueryContext = new ODataQueryContext(model, typeof(DeviceODataQuery));

/* Configure supported OData parameters for validation */
ODataValidationSettings.AllowedFunctions = AllowedFunctions.None;
ODataValidationSettings.AllowedLogicalOperators = AllowedLogicalOperators.GreaterThan;
ODataValidationSettings.AllowedArithmeticOperators = AllowedArithmeticOperators.None;
ODataValidationSettings.AllowedQueryOptions = AllowedQueryOptions.Top | AllowedQueryOptions.Filter;

Long story short, I can now use $filter=DeviceId gt 'someValue' in my requests, but it rejects using the name from the annotation, I cannot use deviceId...

It seems like the annotations are ignored, is there another way to do it or fix my code to make annotations working?

来源:https://stackoverflow.com/questions/33663255/odata-entity-property-serialization-name-different-than-c-sharp-variable-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!