2019-12-16 14:55:59 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Configuration;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-12-13 11:37:58 +00:00
|
|
|
|
|
2019-11-29 12:26:53 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
namespace ASC.Core.Common.EF
|
|
|
|
|
{
|
2019-12-16 14:55:59 +00:00
|
|
|
|
public class BaseDbContext : DbContext
|
2019-11-29 12:26:53 +00:00
|
|
|
|
{
|
2019-12-13 11:37:58 +00:00
|
|
|
|
public BaseDbContext() { }
|
|
|
|
|
|
|
|
|
|
public BaseDbContext(DbContextOptions options) : base(options)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-29 12:26:53 +00:00
|
|
|
|
internal ILoggerFactory LoggerFactory { get; set; }
|
|
|
|
|
internal ConnectionStringSettings ConnectionStringSettings { get; set; }
|
|
|
|
|
|
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
|
|
|
{
|
|
|
|
|
optionsBuilder.UseLoggerFactory(LoggerFactory);
|
|
|
|
|
optionsBuilder.EnableSensitiveDataLogging();
|
|
|
|
|
optionsBuilder.UseMySql(ConnectionStringSettings.ConnectionString);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-16 14:55:59 +00:00
|
|
|
|
|
|
|
|
|
public class MultiRegionalDbContext<T> : IDisposable, IAsyncDisposable where T : BaseDbContext, new()
|
|
|
|
|
{
|
|
|
|
|
public MultiRegionalDbContext() { }
|
|
|
|
|
|
|
|
|
|
internal List<T> Context { get; set; }
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (Context == null) return;
|
|
|
|
|
|
|
|
|
|
foreach (var c in Context)
|
|
|
|
|
{
|
|
|
|
|
if (c != null)
|
|
|
|
|
{
|
|
|
|
|
c.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public async ValueTask DisposeAsync()
|
|
|
|
|
{
|
|
|
|
|
if (Context == null) return;
|
|
|
|
|
|
|
|
|
|
foreach (var c in Context)
|
|
|
|
|
{
|
|
|
|
|
if (c != null)
|
|
|
|
|
{
|
|
|
|
|
await c.DisposeAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-29 12:26:53 +00:00
|
|
|
|
}
|