【问题标题】:How to use OData without the Entity Framework如何在没有实体框架的情况下使用 OData
【发布时间】:2015-02-14 02:12:50
【问题描述】:

我有兴趣使用 Visual Studio 2012 创建 OData wcf 数据服务。但是我不想使用实体模型框架,而是使用我的方案较少的 nosql 数据集来存储和检索数据。 有没有一种方法可以让我控制 odata 服务,而不会陷入特定的类结构,例如 Microsoft 的实体框架。

【问题讨论】:

    标签: visual-studio-2012 wcf-data-services odata


    【解决方案1】:

    您可以在没有实体框架的情况下使用 Microsoft OData 实现。您需要的是IQueryable 的实现。这是查询对象数组的示例 OData 服务:

    using System.Web.Http;
    using System.Web.Http.OData;
    using System.Web.Http.OData.Builder;
    using System.Web.Http.OData.Query;
    
    // GET api/values
    [ActionName("FromList")]
    public IList<Poco> GetFromList(ODataQueryOptions<Poco> queryOptions)
    {
        IQueryable<Poco> data = (
            new Poco[] { 
                new Poco() { id = 1, name = "one", type = "a" },
                new Poco() { id = 2, name = "two", type = "b" },
                new Poco() { id = 3, name = "three", type = "c" }
            })
            .AsQueryable();
    
        var t = new ODataValidationSettings() { MaxTop = 25 };
        queryOptions.Validate(t);
    
        var s = new ODataQuerySettings() { PageSize = 25 };
        IEnumerable<Poco> results =
            (IEnumerable<Poco>)queryOptions.ApplyTo(data, s);
    
        return results.ToList();
    }
    
    public class Poco
    {
        public int id { get; set; }
        public string name { get; set; }
        public string type { get; set; }
    }
    

    【讨论】:

    • 这样,您不会失去在数据库中过滤的能力,而是在内存中执行。无论如何优雅,无需重新实现 IQueryable 接口?
    • @gdp 你在寻找类似Simple.OData的东西吗?
    • 我会看看,但理想情况下我想解析 OData 查询并将其转换为某种模型,我可以在其中动态构建 SQL。我正在处理的项目中的数据层无法返回 IQueryable,我仍然希望在数据库中完成分页/过滤。
    猜你喜欢
    • 1970-01-01
    • 2016-05-19
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    相关资源
    最近更新 更多