|
@@ -1,4 +1,6 @@
|
|
|
using EVCB_OCPP.Domain;
|
|
|
+using EVCB_OCPP.Domain.Extensions;
|
|
|
+using EVCB_OCPP.TaskScheduler.Services;
|
|
|
using Microsoft.Data.SqlClient;
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
using Microsoft.Extensions.Configuration;
|
|
@@ -15,54 +17,6 @@ namespace EVCB_OCPP.TaskScheduler.Helper;
|
|
|
public static class AddPortalDbContext
|
|
|
{
|
|
|
public const string CommandTimeoutKey = "CommandTimeout";
|
|
|
- public static IServiceCollection AddMainDbContext(this IServiceCollection services, IConfiguration configuration)
|
|
|
- {
|
|
|
- const string DbUserIdKey = "MainDbUserIdKey";
|
|
|
- const string DbPassKey = "MainDbPass";
|
|
|
- const string DbConnectionStringKey = "MainDBContext";
|
|
|
-
|
|
|
- var conneciotnString = GetConnectionString(configuration, DbUserIdKey, DbPassKey, DbConnectionStringKey);
|
|
|
-
|
|
|
- services.AddSingleton(
|
|
|
- new SqlConnectionFactory<MainDBContext>()
|
|
|
- {
|
|
|
- ConnectionString = conneciotnString
|
|
|
- });
|
|
|
- AddPortalDbContextInternal<MainDBContext>(services, configuration, conneciotnString, logToConsole: false);
|
|
|
- return services;
|
|
|
- }
|
|
|
-
|
|
|
- public static IServiceCollection AddMeterValueDbContext(this IServiceCollection services, IConfiguration configuration)
|
|
|
- {
|
|
|
- const string DbUserIdKey = "MeterValueDbUserId";
|
|
|
- const string DbPassKey = "MeterValueDbPass";
|
|
|
- const string DbConnectionStringKey = "MeterValueDBContext";
|
|
|
-
|
|
|
- var conneciotnString = GetConnectionString(configuration, DbUserIdKey, DbPassKey, DbConnectionStringKey);
|
|
|
- services.AddSingleton(
|
|
|
- new SqlConnectionFactory<MeterValueDBContext>()
|
|
|
- {
|
|
|
- ConnectionString = conneciotnString
|
|
|
- });
|
|
|
- AddPortalDbContextInternal<MeterValueDBContext>(services, configuration, conneciotnString, logToConsole: false);
|
|
|
- return services;
|
|
|
- }
|
|
|
-
|
|
|
- public static IServiceCollection AddConnectionLogDbContext(this IServiceCollection services, IConfiguration configuration)
|
|
|
- {
|
|
|
- const string DbUserIdKey = "ConnectionLogDbUserId";
|
|
|
- const string DbPassKey = "ConnectionLogDbPass";
|
|
|
- const string DbConnectionStringKey = "ConnectionLogDBContext";
|
|
|
-
|
|
|
- var conneciotnString = GetConnectionString(configuration, DbUserIdKey, DbPassKey, DbConnectionStringKey);
|
|
|
- services.AddSingleton(
|
|
|
- new SqlConnectionFactory<ConnectionLogDBContext>()
|
|
|
- {
|
|
|
- ConnectionString = conneciotnString
|
|
|
- });
|
|
|
- AddPortalDbContextInternal<ConnectionLogDBContext>(services, configuration, conneciotnString);
|
|
|
- return services;
|
|
|
- }
|
|
|
|
|
|
public static IServiceCollection AddWebDBConetext(this IServiceCollection services, IConfiguration configuration)
|
|
|
{
|
|
@@ -70,12 +24,8 @@ public static class AddPortalDbContext
|
|
|
const string DbPassKey = "WebDbPass";
|
|
|
const string DbConnectionStringKey = "WebDBContext";
|
|
|
|
|
|
- var conneciotnString = GetConnectionString(configuration, DbUserIdKey, DbPassKey, DbConnectionStringKey);
|
|
|
- services.AddSingleton(
|
|
|
- new SqlConnectionFactory<WebDBConetext>()
|
|
|
- {
|
|
|
- ConnectionString = conneciotnString
|
|
|
- });
|
|
|
+ var conneciotnString = configuration.GetConnectionString(DbUserIdKey, DbPassKey, DbConnectionStringKey);
|
|
|
+ services.AddSqlConnectionFactory<WebDBConetext>(conneciotnString);
|
|
|
return services;
|
|
|
}
|
|
|
|
|
@@ -85,58 +35,10 @@ public static class AddPortalDbContext
|
|
|
const string DbPassKey = "OnlineLogDbPass";
|
|
|
const string DbConnectionStringKey = "OnlineLogDBContext";
|
|
|
|
|
|
- var conneciotnString = GetConnectionString(configuration, DbUserIdKey, DbPassKey, DbConnectionStringKey);
|
|
|
- services.AddSingleton(
|
|
|
- new SqlConnectionFactory<OnlineLogDBContext>()
|
|
|
- {
|
|
|
- ConnectionString = conneciotnString
|
|
|
- });
|
|
|
+ var conneciotnString = configuration.GetConnectionString(DbUserIdKey, DbPassKey, DbConnectionStringKey);
|
|
|
+ services.AddSqlConnectionFactory<OnlineLogDBContext>(conneciotnString);
|
|
|
return services;
|
|
|
}
|
|
|
- private static void AddPortalDbContextInternal<T>(
|
|
|
- IServiceCollection services, IConfiguration configuration,
|
|
|
- string connectionString, bool logToConsole = false) where T : DbContext
|
|
|
- {
|
|
|
-
|
|
|
- var commandTimeout = int.TryParse(configuration[CommandTimeoutKey], out var temp) ? temp : 180;
|
|
|
-
|
|
|
- services.AddPooledDbContextFactory<T>((serviceProvider, options) => {
|
|
|
- options.UseSqlServer(connectionString, dbOptions =>
|
|
|
- {
|
|
|
- dbOptions.CommandTimeout(commandTimeout);
|
|
|
- });
|
|
|
- if (logToConsole)
|
|
|
- {
|
|
|
- options.LogTo(Console.WriteLine);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- private static string GetConnectionString(IConfiguration configuration, string UserIdKey, string DbPassKey, string ConnectionStringKey)
|
|
|
- {
|
|
|
- string mainDbUserId = string.IsNullOrEmpty(configuration[UserIdKey]) ? string.Empty : $"user id={configuration[UserIdKey]};";
|
|
|
- string mainDbUserPass = string.IsNullOrEmpty(configuration[DbPassKey]) ? string.Empty : $"password={configuration[DbPassKey]};";
|
|
|
- return $"{configuration.GetConnectionString(ConnectionStringKey)}{mainDbUserId}{mainDbUserPass}";
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-public class SqlConnectionFactory<T> where T : DbContext
|
|
|
-{
|
|
|
- public string ConnectionString { get; init; }
|
|
|
- public SqlConnectionFactory() { }
|
|
|
- public SqlConnection Create()
|
|
|
- {
|
|
|
- var sqlConnection = new SqlConnection(ConnectionString);
|
|
|
- sqlConnection.Open();
|
|
|
- return sqlConnection;
|
|
|
- }
|
|
|
-
|
|
|
- public async Task<SqlConnection> CreateAsync()
|
|
|
- {
|
|
|
- var sqlConnection = new SqlConnection(ConnectionString);
|
|
|
- await sqlConnection.OpenAsync();
|
|
|
- return sqlConnection;
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
/// <summary>
|