関連付けに関するこのセグメントに出くわしたとき、Odata.org で OData ソースについて読んでいました。
アソシエーションは、2 つ以上のエンティティ タイプ間の関係を定義します (たとえば、Employee WorksFor Department)。アソシエーションのインスタンスは、アソシエーション セットにグループ化されます。ナビゲーション プロパティは、エンティティ タイプの特別なプロパティであり、特定の関連付けにバインドされ、エンティティの関連付けを参照するために使用できます。
最後に、すべてのインスタンス コンテナー (エンティティ セットとアソシエーション セット) がエンティティ コンテナーにグループ化されます。
上記のパラグラフを 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 });
}
}