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

134 lines
5.3 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
// any third-party rights.
//
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
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 static class BaseDbContextExtension
{
2022-07-29 17:23:19 +00:00
public static void OptionsAction(IServiceProvider sp, DbContextOptionsBuilder optionsBuilder)
2022-07-27 17:12:08 +00:00
{
2022-07-29 17:23:19 +00:00
var configuration = new ConfigurationExtension(sp.GetRequiredService<IConfiguration>());
var migrateAssembly = configuration["testAssembly"];
var connectionString = configuration.GetConnectionStrings("default");
var loggerFactory = sp.GetRequiredService<EFLoggerFactory>();
2022-07-27 17:12:08 +00:00
2022-07-29 17:23:19 +00:00
optionsBuilder.UseLoggerFactory(loggerFactory);
optionsBuilder.EnableSensitiveDataLogging();
2022-07-27 17:12:08 +00:00
2022-07-29 17:23:19 +00:00
var _provider = Provider.MySql;
switch (connectionString.ProviderName)
{
case "MySql.Data.MySqlClient":
_provider = Provider.MySql;
break;
case "Npgsql":
_provider = Provider.PostgreSql;
break;
}
2022-07-27 17:12:08 +00:00
2022-07-29 17:23:19 +00:00
switch (_provider)
{
case Provider.MySql:
optionsBuilder.ReplaceService<IMigrationsSqlGenerator, CustomMySqlMigrationsSqlGenerator>();
optionsBuilder.UseMySql(connectionString.ConnectionString, ServerVersion.Parse("8.0.25"), providerOptions =>
{
if (!string.IsNullOrEmpty(migrateAssembly))
2022-07-27 17:12:08 +00:00
{
2022-07-29 17:23:19 +00:00
providerOptions.MigrationsAssembly(migrateAssembly);
}
2022-07-27 17:12:08 +00:00
2022-07-29 17:23:19 +00:00
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
providerOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
});
break;
case Provider.PostgreSql:
optionsBuilder.UseNpgsql(connectionString.ConnectionString, providerOptions =>
{
if (!string.IsNullOrEmpty(migrateAssembly))
2022-07-27 17:12:08 +00:00
{
2022-07-29 17:23:19 +00:00
providerOptions.MigrationsAssembly(migrateAssembly);
}
});
break;
}
}
public static void AddBaseDbContextPool<T>(this IServiceCollection services) where T : DbContext
{
services.AddPooledDbContextFactory<T>(OptionsAction);
}
public static void AddBaseDbContext<T>(this IServiceCollection services) where T : DbContext
{
services.AddDbContext<T>(OptionsAction);
2022-07-27 17:12:08 +00:00
}
public static T AddOrUpdate<T, TContext>(this TContext b, Expression<Func<TContext, DbSet<T>>> expressionDbSet, T entity) where T : BaseEntity where TContext : DbContext
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
}
}
2022-07-28 18:00:49 +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 : DbContext
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();
}