【问题标题】:OData Associations In C#C# 中的 OData 关联
【发布时间】:2013-06-27 21:03:55
【问题描述】:

我在 Odata.org 上阅读有关 OData 源的信息时,我遇到了这个关于关联的部分。

关联定义了两个或多个实体类型之间的关系 (例如,员工为部门工作)。关联实例 被分组在关联集中。导航属性是特殊的 绑定到特定关联的实体类型的属性 并且可以用来指代实体的关联。

最后,所有实例容器(实体集和关联集) 被分组在一个实体容器中。

将上述段落放入 OData 术语中, OData 服务由实体集或导航属性表示 在标识实体集合的实体类型上。为了 例如,由 URI 标识的实体集 http://services.odata.org/OData/OData.svc/Products 或收藏 由“产品”导航属性标识的实体 http://services.odata.org/OData/OData.svc/Categories(1)/Products 标识由 OData 服务公开的条目提要。

我正在使用 Visual Studio 2012 在 C# 中创建 OData 服务,并希望使用提到的 URL 功能。但是我不知道如何设置它。有人知道怎么做吗?

这是我的代码:

    public class AssociationTest : DataService<ServiceContext>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
        config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
    }
}
public class ServiceContext
{
    CategoryList _categories = new CategoryList();
    public IQueryable<Category> CategorySet
    {
        get{return _categories.AsQueryable();}
    }
    ProductList _products = new ProductList();
    public IQueryable<Product> ProductSet
    {
        get{return _products.AsQueryable();}
    }

}
[System.Data.Services.Common.DataServiceKeyAttribute("ID")]
public class Category
{
    public string Type
    {
        get;
        set;
    }
    public int ID
    {
        get;
        set;
    }
}
public class CategoryList : List<Category>
{
    public CategoryList()
        : base()
    {
        Add(new Category { Type = "Hardware", ID = 0 });
        Add(new Category { Type = "Software", ID = 1 });
    }
}
[System.Data.Services.Common.DataServiceKeyAttribute("ID")]
public class Product
{
    public string Name
    {
        get;
        set;
    }
    public int ID
    {
        get;
        set;
    }
    public int CategoryID
    {
        get;
        set;
    }
}
public class ProductList:List<Product>
{
    public ProductList()
        : base()
    {
        Add(new Product { Name = "Computer", ID = 0, CategoryID = 0 });
        Add(new Product { Name = "Phone", ID = 1, CategoryID = 0 });
        Add(new Product { Name = "Outlook", ID = 2, CategoryID = 1 });
        Add(new Product { Name = "Excel", ID = 3, CategoryID = 1 });
    }
}

【问题讨论】:

  • 您是使用 WCF 数据服务、ASP.NET Web API 还是手工制作 OData 服务器?
  • 如果我正确理解了您的问题,那么我认为您想要的将开箱即用。例如,如果您使用实体框架作为您的提供者,那么遵循快速入门指南here 应该会生成一个服务,该服务会生成带有导航链接的有效负载,就像您复制的文本中的那样。还是我误会了?
  • 这是相同的想法,只是我不想将我的 DataService 基于实体数据模型。我试图以包含 IQueryable 列表的类为基础。我将包含我的一些代码。

标签: c# wcf-data-services odata


【解决方案1】:

您可以让服务模型直接指向产品的类别,而不是使用外键对 Product->Category 之间的关系建模。我的意思是,而不是这个:

public class Product
{
    // ...
    public int CategoryID
    {
        get;
        set;
    }
}

你可以这样建模:

public class Product
{
    // ...
    public Category ProductCategory
    {
        get;
        set;
    }
}

如果您以这种方式设置模型,WCF 数据服务反射提供程序应自动在 Product 上创建导航属性以及模型中的必要关联。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    相关资源
    最近更新 更多