-
Notifications
You must be signed in to change notification settings - Fork 32
Automatically building OData APIs (Dynamic data context)
ASP.NET Core Middleware
For create server from only connection string, add reference to project \source\OdataToEntity.EfCore.DynamicDataContext
- Sql Server
public void Configure(IApplicationBuilder app)
{
var schemaFactory = new DynamicSchemaFactory("sqlserver", "Server=.\\sqlexpress;Initial Catalog=OdataToEntity;Trusted_Connection=Yes;");
using (ProviderSpecificSchema providerSchema = schemaFactory.CreateSchema(useRelationalNulls: true))
{
IEdmModel edmModel = DynamicMiddlewareHelper.CreateEdmModel(providerSchema, informationSchemaMapping: null);
app.UseOdataToEntityMiddleware<OePageMiddleware>("/api", edmModel);
}
}
- PostgreSql
public void Configure(IApplicationBuilder app)
{
var schemaFactory = new DynamicSchemaFactory("postgresql", "Host=localhost;Port=5432;Database=OdataToEntity;Pooling=true");
using (ProviderSpecificSchema providerSchema = schemaFactory.CreateSchema(useRelationalNulls: true))
{
IEdmModel edmModel = DynamicMiddlewareHelper.CreateEdmModel(providerSchema, informationSchemaMapping: null);
app.UseOdataToEntityMiddleware<OePageMiddleware>("/api", edmModel);
}
}
- MySql
public void Configure(IApplicationBuilder app)
{
var schemaFactory = new DynamicSchemaFactory("mysql", "server=localhost;database=dbo;user=root;password=123456");
using (ProviderSpecificSchema providerSchema = schemaFactory.CreateSchema(useRelationalNulls: true))
{
IEdmModel edmModel = DynamicMiddlewareHelper.CreateEdmModel(providerSchema, informationSchemaMapping: null);
app.UseOdataToEntityMiddleware<OePageMiddleware>("/api", edmModel);
}
}
HTTP server
Solution file - \sln\OdataToEntity.Test.DynamicDataContext.sln
Example implementation http server - \test\OdataToEntity.Test.DynamicDataContext.AspServer.
Http server configuration is done through the appsettings.json file.
"OdataToEntity": {
"BasePath" : "api",
"Provider": "sqlserver",
"ConnectionString": "Server=.\\sqlexpress;Initial Catalog=OdataToEntity;Trusted_Connection=Yes;",
"UseRelationalNulls": true,
"InformationSchemaMappingFileName": "InformationSchemaMapping.json"
}
"BasePath" - base path in the server URL.
"Provider" - database type, possible values mysql, postgresql, sqlserver.
"ConnectionString" - connection string to the database.
"UseRelationalNulls" - indicates whether or not to use relational database semantics when comparing null values.
"InformationSchemaMappingFileName" - custom mapping the database to the OData schema.
For additional customization of tables and foreign keys names, used InformationSchemaMapping.json file is a serialized class InformationSchemaMapping.
An example for use in your code
//Load our schema mappings (optional)
InformationSchemaMapping informationSchemaMapping = GetMappings();
var optionsFactory = new DynamicSchemaFactory("sqlserver", @"Server=.\sqlexpress;Initial Catalog=OdataToEntity;Trusted_Connection=Yes;");
//create database schema
using (ProviderSpecificSchema providerSchema = optionsFactory.CreateSchema(useRelationalNulls: true))
using (var metadataProvider = providerSchema.CreateMetadataProvider(informationSchemaMapping))
{
DynamicTypeDefinitionManager typeDefinitionManager = DynamicTypeDefinitionManager.Create(metadataProvider);
//Create adapter data access
var dataAdapter = new DynamicDataAdapter(typeDefinitionManager);
//Build OData edm model
dynamicEdmModel = dataAdapter.BuildEdmModel(metadataProvider);
}
//Create query parser
var parser = new OeParser(new Uri("http://dummy"), dynamicEdmModel);
//Query
var uri = new Uri("http://dummy/Orders?$expand=Customer,Items&$orderby=Id");
//The result of the query
var stream = new MemoryStream();
//Execute query
await parser.ExecuteGetAsync(uri, OeRequestHeaders.JsonDefault, stream, CancellationToken.None);
stream.Position = 0;
//Get result as string
Console.WriteLine(new StreamReader(stream).ReadToEnd());