pavelbannov
320a1f2250
# Conflicts: # common/ASC.Api.Core/Core/BaseStartup.cs # common/ASC.Common/Caching/AscCache.cs # common/ASC.Common/Data/StreamExtension.cs # common/ASC.Common/Utils/RandomString.cs # common/ASC.Core.Common/Billing/CouponManager.cs # common/ASC.Core.Common/Billing/License/LicenseReader.cs # common/ASC.Core.Common/Core/UserGroupRef.cs # common/ASC.Core.Common/Data/DbTenantService.cs # common/ASC.Core.Common/Notify/Jabber/JabberServiceClientWcf.cs # common/ASC.Core.Common/Notify/Telegram/Dao/CachedTelegramDao.cs # common/ASC.Data.Backup.Core/Core/DbHelper.cs # common/ASC.Data.Backup.Core/Storage/BackupRepository.cs # common/ASC.Data.Backup.Core/Tasks/Data/TableInfo.cs # common/ASC.Data.Storage/BaseStorage.cs # common/ASC.Data.Storage/DiscStorage/DiscDataStore.cs # common/ASC.Data.Storage/GoogleCloud/GoogleCloudStorage.cs # common/ASC.Data.Storage/RackspaceCloud/RackspaceCloudStorage.cs # common/ASC.Data.Storage/S3/S3Storage.cs # common/ASC.Notify.Textile/JabberStyler.cs # common/ASC.Textile/Blocks/GlyphBlockModifier.cs # common/ASC.Textile/States/TableRowFormatterState.cs # common/services/ASC.ApiSystem/Classes/CommonMethods.cs # common/services/ASC.ApiSystem/Controllers/PortalController.cs # common/services/ASC.ClearEvents/Program.cs # common/services/ASC.TelegramService/Startup.cs # common/services/ASC.UrlShortener.Svc/Program.cs # products/ASC.Files/Core/Core/Entries/File.cs # products/ASC.Files/Core/Core/Entries/FileEntry.cs # products/ASC.Files/Core/Core/Entries/FileHelper.cs # products/ASC.Files/Core/Core/Entries/Folder.cs # products/ASC.Files/Core/Core/FileStorageService.cs # products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderDaoBase.cs # products/ASC.Files/Core/Helpers/ThirdpartyConfiguration.cs # products/ASC.Files/Core/HttpHandlers/FileHandler.ashx.cs # products/ASC.Files/Core/Services/DocumentService/Configuration.cs # products/ASC.Files/Core/Services/DocumentService/DocumentServiceConnector.cs # products/ASC.Files/Core/Services/DocumentService/DocumentServiceTracker.cs # products/ASC.Files/Core/Services/WCFService/FileOperations/FileDownloadOperation.cs # products/ASC.Files/Core/Services/WCFService/FileOperations/FileMoveCopyOperation.cs # products/ASC.Files/Core/Utils/EntryManager.cs # products/ASC.Files/Server/Helpers/FilesControllerHelper.cs # products/ASC.Files/Server/Startup.cs # products/ASC.Files/Service/Thumbnail/Builder.cs # products/ASC.Files/Service/Thumbnail/FileDataProvider.cs # products/ASC.People/Server/Startup.cs # web/ASC.Web.Core/Files/DocumentService.cs # web/ASC.Web.Core/Files/DocumentServiceLicense.cs # web/ASC.Web.Core/QuotaSync.cs # web/ASC.Web.Core/Sms/SmsKeyStorage.cs # web/ASC.Web.Core/Users/UserManagerWrapper.cs # web/ASC.Web.HealthChecks.UI/Program.cs # web/ASC.Web.Studio/Startup.cs
149 lines
4.4 KiB
C#
149 lines
4.4 KiB
C#
|
|
using DbContext = Microsoft.EntityFrameworkCore.DbContext;
|
|
|
|
namespace ASC.Core.Common.EF
|
|
{
|
|
public enum Provider
|
|
{
|
|
PostgreSql,
|
|
MySql
|
|
}
|
|
|
|
public class BaseDbContext : DbContext
|
|
{
|
|
public BaseDbContext() { }
|
|
public BaseDbContext(DbContextOptions options) : base(options)
|
|
{
|
|
|
|
}
|
|
|
|
internal string MigrateAssembly { get; set; }
|
|
internal ILoggerFactory LoggerFactory { get; set; }
|
|
public ConnectionStringSettings ConnectionStringSettings { get; set; }
|
|
protected internal Provider Provider { get; set; }
|
|
|
|
public static readonly ServerVersion ServerVersion = ServerVersion.Parse("8.0.25");
|
|
protected virtual Dictionary<Provider, Func<BaseDbContext>> ProviderContext
|
|
{
|
|
get { return null; }
|
|
}
|
|
|
|
public void Migrate()
|
|
{
|
|
if (ProviderContext != null)
|
|
{
|
|
var provider = GetProviderByConnectionString();
|
|
|
|
using var sqlProvider = ProviderContext[provider]();
|
|
sqlProvider.ConnectionStringSettings = ConnectionStringSettings;
|
|
sqlProvider.LoggerFactory = LoggerFactory;
|
|
sqlProvider.MigrateAssembly = MigrateAssembly;
|
|
|
|
sqlProvider.Database.Migrate();
|
|
}
|
|
else
|
|
{
|
|
Database.Migrate();
|
|
}
|
|
}
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
optionsBuilder.UseLoggerFactory(LoggerFactory);
|
|
optionsBuilder.EnableSensitiveDataLogging();
|
|
Provider = GetProviderByConnectionString();
|
|
switch (Provider)
|
|
{
|
|
case Provider.MySql:
|
|
optionsBuilder.UseMySql(ConnectionStringSettings.ConnectionString, ServerVersion, r =>
|
|
{
|
|
if (!string.IsNullOrEmpty(MigrateAssembly))
|
|
{
|
|
r.MigrationsAssembly(MigrateAssembly);
|
|
}
|
|
});
|
|
break;
|
|
case Provider.PostgreSql:
|
|
optionsBuilder.UseNpgsql(ConnectionStringSettings.ConnectionString);
|
|
break;
|
|
}
|
|
}
|
|
|
|
public Provider GetProviderByConnectionString()
|
|
{
|
|
switch (ConnectionStringSettings.ProviderName)
|
|
{
|
|
case "MySql.Data.MySqlClient":
|
|
return Provider.MySql;
|
|
case "Npgsql":
|
|
return Provider.PostgreSql;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return Provider.MySql;
|
|
}
|
|
}
|
|
|
|
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
|
|
{
|
|
var dbSet = expressionDbSet.Compile().Invoke(b);
|
|
var existingBlog = dbSet.Find(entity.GetKeys());
|
|
if (existingBlog == null)
|
|
{
|
|
return dbSet.Add(entity).Entity;
|
|
}
|
|
else
|
|
{
|
|
b.Entry(existingBlog).CurrentValues.SetValues(entity);
|
|
return entity;
|
|
}
|
|
}
|
|
}
|
|
|
|
public abstract class BaseEntity
|
|
{
|
|
public abstract object[] GetKeys();
|
|
}
|
|
|
|
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 ValueTask DisposeAsync()
|
|
{
|
|
if (Context == null) return ValueTask.CompletedTask;
|
|
|
|
return InternalDisposeAsync();
|
|
}
|
|
|
|
private async ValueTask InternalDisposeAsync()
|
|
{
|
|
foreach (var c in Context)
|
|
{
|
|
if (c != null)
|
|
{
|
|
await c.DisposeAsync();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|