/// <summary> /// Registers the NKitDbDbContext specified by D and the NKitDbContextRepository specified by R as transient services, /// which make them accessible through the Dependency Injection container and available to all NKit controllers just as the NKitWebApiController and/or NKitWebApiControllerCrud. /// </summary> /// <typeparam name="D">The Entity Framework NKitDbContext to be used to manage the database.</typeparam> /// <param name="services">The DI services container received in the Startup class.</param> /// <param name="configuration">The IConfiguration received in the Startup class.</param> public static void RegisterNKitDbContext <D>( this IServiceCollection services, IConfiguration configuration) where D : NKitDbContext { NKitDbContextSettings dbContextSettings = NKitDbContextSettings.GetSettings(configuration); if (dbContextSettings != null) { services.AddDbContext <D>(ServiceLifetime.Transient); //Register the NKitDbContext. } }
/// <summary> /// Updates database to the latest migration on the given DbContext. /// </summary> public static void UpdateNKitDatabase <D>(this IApplicationBuilder applicationBuilder) where D : DbContext { NKitDbContextSettings dbContextSettings = NKitDbContextSettings.GetSettings(); if (dbContextSettings == null) { throw new Exception($"Cannot update NKitDatabase when {nameof(NKitDbContextSettings)} have not been specified in the {NKitInformation.GetAspNetCoreEnvironmentAppSettingsFileName()} file."); } using (var serviceScope = applicationBuilder.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope()) { using (var context = serviceScope.ServiceProvider.GetService <D>()) { context.Database.Migrate(); } } }