Skip to content

Automatically building OData APIs (Dynamic data context)

Maxim edited this page Jul 17, 2019 · 15 revisions

For create server from only connection string, add reference to project \source\OdataToEntity.EfCore.DynamicDataContext

  • Sql Server
public void Configure(IApplicationBuilder app)
{
    var optionsBuilder = new DbContextOptionsBuilder<DynamicDbContext>();
    optionsBuilder = optionsBuilder.UseSqlServer("Server=.\\sqlexpress;Initial Catalog=OdataToEntity;Trusted_Connection=Yes;");
    using (ProviderSpecificSchema providerSchema = new SqlServerSchema(optionsBuilder.Options))
        app.DynamicMiddleware("/api", providerSchema, null);
}
  • PostgreSql
public void Configure(IApplicationBuilder app)
{
    var optionsBuilder = new DbContextOptionsBuilder<DynamicDbContext>();
    optionsBuilder = optionsBuilder.UseNpgsql("Host=localhost;Port=5432;Database=OdataToEntity;Pooling=true;User Id=mvoronov");
    using (ProviderSpecificSchema providerSchema = new PostgreSqlSchema(optionsBuilder.Options))
        app.DynamicMiddleware("/api", providerSchema, null);
}
  • MySql
public void Configure(IApplicationBuilder app)
{
    var optionsBuilder = new DbContextOptionsBuilder<DynamicDbContext>();
    optionsBuilder = optionsBuilder.UseMySql("server=localhost;database=dbo;user=root;password=123456");
    using (ProviderSpecificSchema providerSchema = new MySqlSchema(optionsBuilder.Options))
        app.DynamicMiddleware("/api", providerSchema, null);
}

Example implementation http server - \test\OdataToEntity.Test.DynamicDataContext.AspServer
Solution file - \sln\OdataToEntity.Test.DynamicDataContext.sln

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" - is the 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.

Clone this wiki locally