Add configuration type binding and implement database connection, disable NativeAOT.
Add: binding setting file items to `AppSettings` class so to provide DI access as `IOptions<OptixServeSettings>`. Add: EF Core and DbContext to access database in services. This results in disabling NativeAOT due to poor supports for *pre-compiled query*, however many design are optimized for AOT for later re-adoption. Add: `DesignTimeDbContextFactory` to support EF Core migrations in NativeAOT. (Kept for re-enabling AOT.) Add: `DbInitializer` for ensuring database connecting in startup. Add: `ConfigurationHelper.CreateDefaultBuilder()` to read configuration files in default locations. Note this method is currently ONLY used by `DesignTimeDbContextFactory`. Refactor is expected. Add: `CommonErrorDto` for simple error message. Add: `VersionEndpoint` ONLY for debugging and testing purpose. Verylikely to be removed in the future. Other: many utilities and fixes easy to understand. Note: EF Core migrations are excluded in the early development. Not expected to be added in version control before v1.0 beta.
This commit is contained in:
34
OptixServe.Api/Utilites/DatabaseHelper.cs
Normal file
34
OptixServe.Api/Utilites/DatabaseHelper.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using OptixServe.Api.Configuration;
|
||||
using OptixServe.Core.Data;
|
||||
|
||||
namespace OptixServe.Api.Utilites;
|
||||
|
||||
public static class DatabaseHelper
|
||||
{
|
||||
public static string BuildConnectionString(DatabaseSettings dbSettings)
|
||||
{
|
||||
return dbSettings.Type switch
|
||||
{
|
||||
DatabaseType.Sqlite => $"Data Source={dbSettings.Host ?? "optixserve.db"}",
|
||||
DatabaseType.MySQL => throw new NotSupportedException("MySQL connection is not yet implemented"),
|
||||
_ => throw new NotSupportedException($"Database type {dbSettings.Type} is not supported")
|
||||
};
|
||||
}
|
||||
|
||||
public static void ConfigureDbContext(DbContextOptionsBuilder options, DatabaseSettings dbSettings)
|
||||
{
|
||||
if (dbSettings?.Type == DatabaseType.Sqlite)
|
||||
{
|
||||
var dbPath = dbSettings.Host ?? "optixserve.db";
|
||||
var connectionString = $"Data Source={dbPath}";
|
||||
|
||||
options.UseSqlite(connectionString, b => b.MigrationsAssembly("OptixServe.Api"));
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotImplementedException("Only SQLite database is currently supported");
|
||||
}
|
||||
}
|
||||
}
|
20
OptixServe.Api/Utilites/DesignTimeDbContextFactory.cs
Normal file
20
OptixServe.Api/Utilites/DesignTimeDbContextFactory.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using OptixServe.Api.Configuration;
|
||||
using OptixServe.Core.Data;
|
||||
|
||||
namespace OptixServe.Api.Utilites;
|
||||
|
||||
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
|
||||
{
|
||||
public AppDbContext CreateDbContext(string[] args)
|
||||
{
|
||||
var configuration = ConfigurationHelper.CreateDefaultBuilder().Build();
|
||||
|
||||
var dbSettings = configuration.GetSection("OptixServe:Database").Get<DatabaseSettings>()!;
|
||||
var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
|
||||
DatabaseHelper.ConfigureDbContext(optionsBuilder, dbSettings);
|
||||
|
||||
return new AppDbContext(optionsBuilder.Options);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user