如何在没有实体框架的情况下使用OData

时间:2013-05-23 09:16:32

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

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

1 个答案:

答案 0 :(得分:4)

您可以在没有Entity Framework的情况下使用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; }
}