CodeLiturgy.Dashboard/BlueWest.Api/StartupExtensions.cs

55 lines
2.1 KiB
C#
Raw Normal View History

2022-08-13 06:35:36 +03:00
using System;
2022-08-19 19:47:35 +03:00
using BlueWest.WebApi.MySQL;
using Microsoft.AspNetCore.Hosting;
2022-08-13 06:35:36 +03:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
2022-08-19 19:47:35 +03:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2022-08-19 06:18:50 +03:00
using Microsoft.Extensions.Logging;
2022-08-13 06:35:36 +03:00
2022-08-19 19:47:35 +03:00
namespace BlueWest.WebApi
2022-08-13 06:35:36 +03:00
{
2022-08-19 19:47:35 +03:00
/// <summary>
/// Startup Extensions
/// </summary>
public static class StartupExtensions
2022-08-13 06:35:36 +03:00
{
2022-08-19 19:47:35 +03:00
/// <summary>
/// Get MYSQL Connection String
/// </summary>
/// <param name="optionsBuilder"></param>
/// <param name="configuration"></param>
public static void GetMySqlSettings(
this DbContextOptionsBuilder optionsBuilder,
IConfiguration configuration,
IWebHostEnvironment environment)
{
var optionsBuilderRef = optionsBuilder.UseMySql(configuration.GetConnectionString("LocalMySQL"),
new MySqlServerVersion(new Version(8, 0, 11)));
2022-08-19 06:18:50 +03:00
// The following three options help with debugging, but should
// be changed or removed for production.
2022-08-19 19:47:35 +03:00
if (environment.IsDevelopment())
{
optionsBuilderRef
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
}
}
/// <summary>
/// Setup database Contexts
/// </summary>
/// <param name="serviceCollection"></param>
/// <param name="configuration"></param>
/// <returns></returns>
public static IServiceCollection PrepareDatabasePool(this IServiceCollection serviceCollection,
IConfiguration configuration, IWebHostEnvironment environment)
{
return serviceCollection
.AddDbContextPool<CountryDbContext>(options => options.GetMySqlSettings(configuration, environment))
.AddDbContextPool<UserDbContext>(options => options.GetMySqlSettings(configuration, environment))
.AddDbContextPool<FinanceDbContext>(options => options.GetMySqlSettings(configuration, environment));
}
2022-08-13 06:35:36 +03:00
}
}