No Description

DatabaseConfiguration.cs 1.1KB

123456789101112131415161718192021222324252627282930313233
  1. using Backend.Data.Interfaces;
  2. using System;
  3. using Microsoft.AspNetCore.Builder;
  4. using Microsoft.EntityFrameworkCore;
  5. using Microsoft.Extensions.Configuration;
  6. using Microsoft.Extensions.DependencyInjection;
  7. namespace Backend.Data.Config
  8. {
  9. public static class DatabaseConfiguration
  10. {
  11. public static IServiceCollection AddDatabaseModule(this IServiceCollection @this, IConfiguration configuration)
  12. {
  13. var connection = configuration.GetConnectionString("AppDbContext");
  14. @this.AddDbContextPool<ApplicationDatabaseContext>(options => options.UseMySql(connection, ServerVersion.AutoDetect(connection)));
  15. return @this;
  16. }
  17. public static IApplicationBuilder UseApplicationDatabase(this IApplicationBuilder @this,
  18. IServiceProvider serviceProvider)
  19. {
  20. using (var scope = serviceProvider.CreateScope())
  21. {
  22. var context = scope.ServiceProvider.GetRequiredService<IApplicationDatabaseContext>();
  23. context.Database.OpenConnection();
  24. context.Database.Migrate();
  25. }
  26. return @this;
  27. }
  28. }
  29. }

Powered by TurnKey Linux.