Skip to content

Instantly share code, notes, and snippets.

@raghuramn
Created November 15, 2012 20:44
Show Gist options
  • Save raghuramn/4081136 to your computer and use it in GitHub Desktop.
Save raghuramn/4081136 to your computer and use it in GitHub Desktop.
Complex Collections using ODataModelBuilder
using Microsoft.Data.Edm;
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Web.Http;
using System.Web.Http.OData.Builder;
using System.Web.Http.OData.Formatter;
using System.Web.Http.SelfHost;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ODataModelBuilder builder = new ODataModelBuilder();
builder.EntitySet<Foo>("foos");
var foo = builder.Entity<Foo>();
foo.HasKey(f => f.ID);
foo.CollectionProperty(f => f.CollectionOne);
foo.CollectionProperty(f => f.CollectionTwo);
IEdmModel model = builder.GetEdmModel();
HttpSelfHostConfiguration config = new HttpSelfHostConfiguration("http://localhost:8085/");
config.HostNameComparisonMode = HostNameComparisonMode.Exact;
config.Routes.MapHttpRoute("$metadata", "$metadata", new { Controller = "ODataMetadata", Action = "GetMetadata" });
config.Formatters.Insert(0, new ODataMediaTypeFormatter(model));
HttpSelfHostServer server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();
Console.WriteLine("started...");
Console.ReadKey();
}
}
public class Foo
{
public int ID { get; set; }
public Bar[] CollectionOne { get; set; }
public Dictionary<string, Bar> CollectionTwo { get; set; }
}
public class Bar
{
public string Value1 { get; set; }
public string Value2 { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment