2019-12-16 14:55:59 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Configuration;
|
2019-12-17 13:01:59 +00:00
|
|
|
|
using System.Linq.Expressions;
|
2019-12-16 14:55:59 +00:00
|
|
|
|
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
|
|
|
|
|
{
|
2020-09-02 15:23:39 +00:00
|
|
|
|
public enum Provider
|
|
|
|
|
{
|
2021-10-12 10:23:20 +00:00
|
|
|
|
PostgreSql,
|
2020-09-02 15:23:39 +00:00
|
|
|
|
MySql
|
|
|
|
|
}
|
|
|
|
|
|
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() { }
|
2020-09-02 15:23:39 +00:00
|
|
|
|
public BaseDbContext(DbContextOptions options) : base(options)
|
|
|
|
|
{
|
2020-10-06 07:06:18 +00:00
|
|
|
|
|
2020-09-02 15:23:39 +00:00
|
|
|
|
}
|
2019-12-13 11:37:58 +00:00
|
|
|
|
|
2020-12-25 10:48:04 +00:00
|
|
|
|
internal string MigrateAssembly { get; set; }
|
2019-11-29 12:26:53 +00:00
|
|
|
|
internal ILoggerFactory LoggerFactory { get; set; }
|
2021-10-07 09:51:29 +00:00
|
|
|
|
public ConnectionStringSettings ConnectionStringSettings { get; set; }
|
2020-10-05 09:27:15 +00:00
|
|
|
|
protected internal Provider Provider { get; set; }
|
2019-11-29 12:26:53 +00:00
|
|
|
|
|
2022-01-19 14:16:15 +00:00
|
|
|
|
public static readonly ServerVersion ServerVersion = ServerVersion.Parse("8.0.25");
|
2020-10-06 07:06:18 +00:00
|
|
|
|
protected virtual Dictionary<Provider, Func<BaseDbContext>> ProviderContext
|
|
|
|
|
{
|
|
|
|
|
get { return null; }
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 10:48:04 +00:00
|
|
|
|
public void Migrate()
|
2020-10-06 07:06:18 +00:00
|
|
|
|
{
|
|
|
|
|
if (ProviderContext != null)
|
|
|
|
|
{
|
|
|
|
|
var provider = GetProviderByConnectionString();
|
|
|
|
|
|
2020-10-08 08:42:57 +00:00
|
|
|
|
using var sqlProvider = ProviderContext[provider]();
|
2020-10-06 07:06:18 +00:00
|
|
|
|
sqlProvider.ConnectionStringSettings = ConnectionStringSettings;
|
|
|
|
|
sqlProvider.LoggerFactory = LoggerFactory;
|
2020-12-25 10:48:04 +00:00
|
|
|
|
sqlProvider.MigrateAssembly = MigrateAssembly;
|
2020-10-06 07:06:18 +00:00
|
|
|
|
|
|
|
|
|
sqlProvider.Database.Migrate();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Database.Migrate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-29 12:26:53 +00:00
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
|
|
|
{
|
|
|
|
|
optionsBuilder.UseLoggerFactory(LoggerFactory);
|
|
|
|
|
optionsBuilder.EnableSensitiveDataLogging();
|
2021-12-08 19:18:04 +00:00
|
|
|
|
Provider = GetProviderByConnectionString();
|
2020-10-06 07:06:18 +00:00
|
|
|
|
switch (Provider)
|
2020-09-02 15:23:39 +00:00
|
|
|
|
{
|
2020-10-06 07:06:18 +00:00
|
|
|
|
case Provider.MySql:
|
2021-09-03 14:02:52 +00:00
|
|
|
|
optionsBuilder.UseMySql(ConnectionStringSettings.ConnectionString, ServerVersion, r =>
|
2020-12-25 10:48:04 +00:00
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(MigrateAssembly))
|
|
|
|
|
{
|
|
|
|
|
r = r.MigrationsAssembly(MigrateAssembly);
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-09-02 15:23:39 +00:00
|
|
|
|
break;
|
2021-10-12 10:23:20 +00:00
|
|
|
|
case Provider.PostgreSql:
|
2020-09-02 15:23:39 +00:00
|
|
|
|
optionsBuilder.UseNpgsql(ConnectionStringSettings.ConnectionString);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-11-29 12:26:53 +00:00
|
|
|
|
}
|
2020-10-06 07:06:18 +00:00
|
|
|
|
|
|
|
|
|
public Provider GetProviderByConnectionString()
|
|
|
|
|
{
|
|
|
|
|
switch (ConnectionStringSettings.ProviderName)
|
|
|
|
|
{
|
|
|
|
|
case "MySql.Data.MySqlClient":
|
|
|
|
|
return Provider.MySql;
|
|
|
|
|
case "Npgsql":
|
2021-10-12 10:23:20 +00:00
|
|
|
|
return Provider.PostgreSql;
|
2020-10-06 07:06:18 +00:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Provider.MySql;
|
|
|
|
|
}
|
2019-11-29 12:26:53 +00:00
|
|
|
|
}
|
2019-12-16 14:55:59 +00:00
|
|
|
|
|
2019-12-17 13:01:59 +00:00
|
|
|
|
public static class BaseDbContextExtension
|
|
|
|
|
{
|
2020-02-21 09:58:40 +00:00
|
|
|
|
public static T AddOrUpdate<T, TContext>(this TContext b, Expression<Func<TContext, DbSet<T>>> expressionDbSet, T entity) where T : BaseEntity where TContext : BaseDbContext
|
2019-12-17 13:01:59 +00:00
|
|
|
|
{
|
|
|
|
|
var dbSet = expressionDbSet.Compile().Invoke(b);
|
|
|
|
|
var existingBlog = dbSet.Find(entity.GetKeys());
|
|
|
|
|
if (existingBlog == null)
|
|
|
|
|
{
|
2020-02-21 09:58:40 +00:00
|
|
|
|
return dbSet.Add(entity).Entity;
|
2019-12-17 13:01:59 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
b.Entry(existingBlog).CurrentValues.SetValues(entity);
|
2020-02-21 09:58:40 +00:00
|
|
|
|
return entity;
|
2019-12-17 13:01:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract class BaseEntity
|
|
|
|
|
{
|
2020-02-21 11:23:56 +00:00
|
|
|
|
public abstract object[] GetKeys();
|
2019-12-17 13:01:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-11 15:37:19 +00:00
|
|
|
|
|
|
|
|
|
public ValueTask DisposeAsync()
|
2019-12-16 14:55:59 +00:00
|
|
|
|
{
|
2022-01-11 15:37:19 +00:00
|
|
|
|
if (Context == null) return ValueTask.CompletedTask;
|
2019-12-16 14:55:59 +00:00
|
|
|
|
|
2022-01-11 15:37:19 +00:00
|
|
|
|
return InternalDisposeAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async ValueTask InternalDisposeAsync()
|
|
|
|
|
{
|
2019-12-16 14:55:59 +00:00
|
|
|
|
foreach (var c in Context)
|
|
|
|
|
{
|
|
|
|
|
if (c != null)
|
|
|
|
|
{
|
|
|
|
|
await c.DisposeAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-29 12:26:53 +00:00
|
|
|
|
}
|