DocSpace-buildtools/common/ASC.Core.Common/EF/Context/BaseDbContext.cs

161 lines
4.5 KiB
C#
Raw Normal View History

2022-02-15 11:52:43 +00:00
using DbContext = Microsoft.EntityFrameworkCore.DbContext;
2019-11-29 12:26:53 +00:00
2022-02-15 11:52:43 +00:00
namespace ASC.Core.Common.EF;
public enum Provider
2019-11-29 12:26:53 +00:00
{
2022-02-15 11:52:43 +00:00
PostgreSql,
MySql
}
2020-09-02 15:23:39 +00:00
2022-02-15 11:52:43 +00:00
public class BaseDbContext : DbContext
{
public BaseDbContext() { }
public BaseDbContext(DbContextOptions options) : base(options) { }
2019-12-13 11:37:58 +00:00
internal string MigrateAssembly;
internal ILoggerFactory LoggerFactory;
2022-02-15 11:52:43 +00:00
public ConnectionStringSettings ConnectionStringSettings { get; set; }
protected internal Provider Provider;
2019-11-29 12:26:53 +00:00
2022-02-15 11:52:43 +00:00
public static readonly ServerVersion ServerVersion = ServerVersion.Parse("8.0.25");
protected virtual Dictionary<Provider, Func<BaseDbContext>> ProviderContext => null;
2020-10-06 07:06:18 +00:00
2022-02-15 11:52:43 +00:00
public void Migrate()
{
if (ProviderContext != null)
2020-10-06 07:06:18 +00:00
{
2022-02-15 11:52:43 +00:00
var provider = GetProviderByConnectionString();
2020-10-06 07:06:18 +00:00
2022-02-15 11:52:43 +00:00
using var sqlProvider = ProviderContext[provider]();
sqlProvider.ConnectionStringSettings = ConnectionStringSettings;
sqlProvider.LoggerFactory = LoggerFactory;
sqlProvider.MigrateAssembly = MigrateAssembly;
2020-10-06 07:06:18 +00:00
2022-02-15 11:52:43 +00:00
sqlProvider.Database.Migrate();
}
else
{
Database.Migrate();
2020-10-06 07:06:18 +00:00
}
2022-02-15 11:52:43 +00:00
}
2020-10-06 07:06:18 +00:00
2022-02-15 11:52:43 +00:00
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseLoggerFactory(LoggerFactory);
optionsBuilder.EnableSensitiveDataLogging();
Provider = GetProviderByConnectionString();
switch (Provider)
2019-11-29 12:26:53 +00:00
{
2022-02-15 11:52:43 +00:00
case Provider.MySql:
optionsBuilder.UseMySql(ConnectionStringSettings.ConnectionString, ServerVersion, r =>
{
if (!string.IsNullOrEmpty(MigrateAssembly))
2020-12-25 10:48:04 +00:00
{
2022-02-15 11:52:43 +00:00
r.MigrationsAssembly(MigrateAssembly);
}
});
break;
case Provider.PostgreSql:
optionsBuilder.UseNpgsql(ConnectionStringSettings.ConnectionString);
break;
2019-11-29 12:26:53 +00:00
}
2022-02-15 11:52:43 +00:00
}
2020-10-06 07:06:18 +00:00
2022-02-15 11:52:43 +00:00
public Provider GetProviderByConnectionString()
{
switch (ConnectionStringSettings.ProviderName)
2020-10-06 07:06:18 +00:00
{
2022-02-15 11:52:43 +00:00
case "MySql.Data.MySqlClient":
return Provider.MySql;
case "Npgsql":
return Provider.PostgreSql;
default:
break;
2020-10-06 07:06:18 +00:00
}
2022-02-15 11:52:43 +00:00
return Provider.MySql;
2019-11-29 12:26:53 +00:00
}
2022-02-15 11:52:43 +00:00
}
2019-12-16 14:55:59 +00:00
2022-02-15 11:52:43 +00:00
public static class BaseDbContextExtension
{
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
{
2022-02-15 11:52:43 +00:00
var dbSet = expressionDbSet.Compile().Invoke(b);
var existingBlog = dbSet.Find(entity.GetKeys());
if (existingBlog == null)
2019-12-17 13:01:59 +00:00
{
2022-02-15 11:52:43 +00:00
return dbSet.Add(entity).Entity;
}
else
{
b.Entry(existingBlog).CurrentValues.SetValues(entity);
return entity;
2019-12-17 13:01:59 +00:00
}
}
public static async Task<T> AddOrUpdateAsync<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 = await dbSet.FindAsync(entity.GetKeys());
if (existingBlog == null)
{
var entityEntry = await dbSet.AddAsync(entity);
return entityEntry.Entity;
}
else
{
2022-02-15 11:52:43 +00:00
b.Entry(existingBlog).CurrentValues.SetValues(entity);
2022-02-15 11:52:43 +00:00
return entity;
2019-12-17 13:01:59 +00:00
}
}
2022-02-15 11:52:43 +00:00
}
2019-12-17 13:01:59 +00:00
2022-02-15 11:52:43 +00:00
public abstract class BaseEntity
{
public abstract object[] GetKeys();
}
2019-12-17 13:01:59 +00:00
2022-02-15 11:52:43 +00:00
public class MultiRegionalDbContext<T> : IDisposable, IAsyncDisposable where T : BaseDbContext, new()
{
public MultiRegionalDbContext() { }
2019-12-16 14:55:59 +00:00
internal List<T> Context;
2019-12-16 14:55:59 +00:00
2022-02-15 11:52:43 +00:00
public void Dispose()
{
if (Context == null)
2019-12-16 14:55:59 +00:00
{
2022-02-15 11:52:43 +00:00
return;
}
2019-12-16 14:55:59 +00:00
2022-02-15 11:52:43 +00:00
foreach (var c in Context)
{
if (c != null)
2019-12-16 14:55:59 +00:00
{
2022-02-15 11:52:43 +00:00
c.Dispose();
2019-12-16 14:55:59 +00:00
}
}
2022-02-15 11:52:43 +00:00
}
2022-01-11 15:37:19 +00:00
2022-02-15 11:52:43 +00:00
public ValueTask DisposeAsync()
{
return Context == null ? ValueTask.CompletedTask : InternalDisposeAsync();
}
2022-01-11 15:37:19 +00:00
2022-02-15 11:52:43 +00:00
private async ValueTask InternalDisposeAsync()
{
foreach (var c in Context)
2022-01-11 15:37:19 +00:00
{
2022-02-15 11:52:43 +00:00
if (c != null)
2019-12-16 14:55:59 +00:00
{
2022-02-15 11:52:43 +00:00
await c.DisposeAsync();
2019-12-16 14:55:59 +00:00
}
}
}
2019-11-29 12:26:53 +00:00
}