No Description

DatabaseConfiguration.cs 1.2KB

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

Powered by TurnKey Linux.