I have written an extension for the ServiceCollection
in which I want to configure my EntityFramework Core configuration and the respective DbContext. Loading the configuarion via a json-file works. Now, the part where I am struggling is that I want to map my IInstanceConfiguration
to my OracleConfiguration
, as a full instance. My current solution looks like this.
I added two comments where I would like to know the correct ( / better ) way to implement this.
Kind regards!
public static void RegisterEfCoreOracle<T>(this IServiceCollection services, string configurationDirectory, string configurationFile) where T : DbContext
{
//Adding configuration file
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(configurationDirectory)
.AddJsonFile(configurationFile, optional: false)
.Build();
services.Configure<OracleConfiguration>(configuration);
//Do i really have to do it manually this way? What would be another, elegant way?
var oraInstance = new OracleConfiguration(configuration.GetValue<string>("Name"), configuration.GetValue<string>("DataSource"), configuration.GetValue<string>("UserId"),
configuration.GetValue<string>("Password"),configuration.GetValue<bool>("UseConsoleLogging"), configuration.GetValue<string>("Compatibility"));
//This is the line where I am struggling.
services.AddSingleton<IInstanceConfiguration, OracleConfiguration>(oraInstance);
var oraConfig = services.ReturnServiceProvider().GetRequiredService<OracleConfiguration>();
services.AddDbContext<T>();
}
public static ServiceProvider ReturnServiceProvider(this IServiceCollection services) => services.BuildServiceProvider();