I was reading about OData sources at Odata.org when I ran across this segment about associations.
Associations define the relationship between two or more Entity Types (for example, Employee WorksFor Department). Instances of associations are grouped in Association Sets. Navigation Properties are special properties on Entity Types which are bound to a specific association and can be used to refer to associations of an entity.
Finally, all instance containers (Entity Sets and Association Sets) are grouped in an Entity Container.
Putting the above paragraphs into OData terms, the feeds exposed by an OData service are represented by Entity Sets or a Navigation Property on an Entity Type that identifies a collection of entities. For example, the Entity Set identified by the URI http://services.odata.org/OData/OData.svc/Products or the collection of entities identified by the "Products" navigation property in http://services.odata.org/OData/OData.svc/Categories(1)/Products identifies a feed of entries exposed by the OData service.
I am making a OData service in C# using Visual Studio 2012 and would like to use the URL functionality mentioned. However I do not know how to set it up. Does anybody know how to do this?
Here is my code:
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 });
}
}