c# - 如何查找 OData edm 模型类型到 clr 类型的映射?

标签 c# .net asp.net-mvc asp.net-web-api odata

我的数据层不公开 IQueryable。我仍然希望能够对数据进行排序。我的 API 具有这样的功能,但我需要确定需要排序的内容。假设我配置了模型,例如:

ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<Airline>("Airlines");
modelBuilder.EntitySet<FlightLeg>("Legs");

现在在 Controller 中,我可以获取 OdataQueryOptions 并找出请求的 edm 模型类型/属性排序。我需要完成的是获取用于配置的 CLR 类型中的属性的 PropertyInfo,例如航空公司.PlaneModel。

我努力通过 API 获取元数据,但没有成功。请帮忙。

最佳答案

在 Controller 中尝试以下代码片段:

        //using Microsoft.Data.Edm
        IEdmModel edmModel = Request.ODataProperties().Model;
        ClrTypeAnnotation annotation = edmModel.GetAnnotationValue<ClrTypeAnnotation>(edmSchemaType);
        if (annotation != null)
        {
            return annotation.ClrType;
        }

关于c# - 如何查找 OData edm 模型类型到 clr 类型的映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24149106/

相关文章:

.net - 如何使用流利验证来验证包含更多相同类型对象的对象?

javascript - 在 ASP.NET MVC 应用程序中使用 AngularJS 发布表单

c# - 如何从 URL 流式传输/下载和播放音频?

c# - Chrome 圆 Angular css?

c# - Automapper 自定义内联映射

c# - 发送多个参数到触发的Azure webjos

c# - 接口(interface)可以需要一个属性,但不指定所需的类型吗?

c# - WCF & CaSTLe Windsor - 看起来你忘了

c# - Entity Framework Code First,不同上下文/数据库之间的导航属性

c# - 服务器和客户端验证差异增加了额外的跨度