public static IEdmModel GetEdmModel(string connectionString, TableControllerSelector selector) { EdmModel model = new EdmModel(); // create and add entity container EdmEntityContainer container = new EdmEntityContainer("NS", "DefaultContainer"); model.AddElement(container); // https://www.nuget.org/packages/DatabaseSchemaReader/ using (var dbReader = new DatabaseReader(connectionString, "System.Data.SqlClient")) { var schema = dbReader.ReadAll(); foreach (var table in schema.Tables) { EdmEntityType tableType = new EdmEntityType("NS", table.Name); foreach (var col in table.Columns) { var kind = GetKind(col); if (!kind.HasValue) // don't map this { continue; } var prop = tableType.AddStructuralProperty(col.Name, kind.Value, col.Nullable); if (col.IsPrimaryKey) { tableType.AddKeys(prop); } } model.AddElement(tableType); EdmEntitySet products = container.AddEntitySet(table.Name, tableType); selector.AddTable(connectionString, table); } } return(model); }
public static void Configuration(IAppBuilder builder) { HttpConfiguration configuration = new HttpConfiguration(); // create a special dynamic controller selector selector = new TableControllerSelector(configuration); IEdmModel model = TableController.GetEdmModel(connectionString, selector); configuration.Services.Replace(typeof(IHttpControllerSelector), selector); configuration.MapODataServiceRoute("odata", "odata", model); // needs using System.Web.OData.Extensions builder.UseWebApi(configuration); }
public static void Register(HttpConfiguration config) { int maxServerPageSize = Convert.ToInt32(WebConfigurationManager.AppSettings["maxServerPageSize"]); AzdaraInit(); config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; // create a special dynamic controller selector TableControllerSelector selector = new TableControllerSelector(config); config.Services.Replace(typeof(IHttpControllerSelector), selector); //Use global configuration config.Count().Filter().OrderBy().Expand().Select().MaxTop(null); // enable query options for all properties config.AddODataQueryFilter(new EnableQueryAttribute() { PageSize = maxServerPageSize, AllowedQueryOptions = Microsoft.AspNet.OData.Query.AllowedQueryOptions.All, AllowedFunctions = Microsoft.AspNet.OData.Query.AllowedFunctions.All, AllowedArithmeticOperators = Microsoft.AspNet.OData.Query.AllowedArithmeticOperators.All, AllowedLogicalOperators = Microsoft.AspNet.OData.Query.AllowedLogicalOperators.All, //HandleNullPropagation = Microsoft.AspNet.OData.Query.HandleNullPropagationOption.True }); //строка для включения null значений в открытых типах, пример свойство в сущности public IDictionary<string, object> DynamicProperties { get; set; } config.Properties.AddOrUpdate("System.Web.OData.NullDynamicPropertyKey", val => true, (oldVal, newVal) => true); //Activate a key as Segment. Then request an entity with key as segment, the URL will be like ~/EntitySet/KeyValue or old ~/EntitySet(KeyValue) //Note : If entity type has composite key, then key as segment is not supported for this entity type config.SetUrlKeyDelimiter(Microsoft.OData.ODataUrlKeyDelimiter.Slash); config.SetTimeZoneInfo(TimeZoneInfo.Utc); // Конфигурация и службы веб-API IEdmModel EDMmodel = Metadata(); // Web API configuration and services // Web API routes config.MapHttpAttributeRoutes(); // Create the default collection of built-in conventions. //var conventions = Microsoft.AspNet.OData.Routing.Conventions.ODataRoutingConventions.CreateDefault(); // Insert the custom convention at the start of the collection. //conventions.Insert(0, new NavigationIndexRoutingConvention()); config.MapODataServiceRoute( routeName: "ODataRoute", routePrefix: "odata", model: EDMmodel //,pathHandler: new Microsoft.AspNet.OData.Routing.DefaultODataPathHandler(), //routingConventions: conventions ); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); // below code allows endpoints to respond with either XML or JSON, depending on accept header preferences sent from client // (default in absence of accept header is JSON) //https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/json-and-xml-serialization var odataFormatters = ODataMediaTypeFormatters.Create(); config.Formatters.InsertRange(0, odataFormatters); }