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

71 lines
2.4 KiB
C#
Raw Normal View History

2019-12-16 14:55:59 +00:00
using System;
using System.Linq;
2020-10-19 07:19:32 +00:00
using ASC.Common;
2019-12-16 14:55:59 +00:00
using ASC.Common.Logging;
2019-11-29 12:26:53 +00:00
using ASC.Common.Utils;
2019-12-13 11:37:58 +00:00
2019-11-29 12:26:53 +00:00
using Microsoft.Extensions.Options;
namespace ASC.Core.Common.EF
{
2020-10-19 07:19:32 +00:00
[Scope]
2020-10-20 13:24:46 +00:00
public class ConfigureDbContext<T> : IConfigureNamedOptions<T> where T : BaseDbContext, new()
2019-11-29 12:26:53 +00:00
{
2020-10-06 07:06:18 +00:00
public const string baseName = "default";
2020-08-12 09:58:08 +00:00
private EFLoggerFactory LoggerFactory { get; }
2020-11-24 10:15:11 +00:00
private ConfigurationExtension Configuration { get; }
2019-11-29 12:26:53 +00:00
2020-11-24 10:15:11 +00:00
public ConfigureDbContext(EFLoggerFactory loggerFactory, ConfigurationExtension configuration)
2019-11-29 12:26:53 +00:00
{
LoggerFactory = loggerFactory;
Configuration = configuration;
}
2020-10-20 13:24:46 +00:00
public void Configure(string name, T context)
2019-11-29 12:26:53 +00:00
{
context.LoggerFactory = LoggerFactory;
context.ConnectionStringSettings = Configuration.GetConnectionStrings(name) ?? Configuration.GetConnectionStrings(baseName);
2020-12-25 10:48:04 +00:00
context.MigrateAssembly = Configuration["testAssembly"];
2019-11-29 12:26:53 +00:00
}
2020-10-20 13:24:46 +00:00
public void Configure(T context)
2019-11-29 12:26:53 +00:00
{
Configure(baseName, context);
}
}
2019-12-16 14:55:59 +00:00
public class ConfigureMultiRegionalDbContext<T> : IConfigureNamedOptions<MultiRegionalDbContext<T>> where T : BaseDbContext, new()
{
2020-10-06 07:06:18 +00:00
public string baseName = "default";
2020-11-24 10:15:11 +00:00
private ConfigurationExtension Configuration { get; }
2020-08-12 09:58:08 +00:00
private DbContextManager<T> DbContext { get; }
2019-12-16 14:55:59 +00:00
2020-11-24 10:15:11 +00:00
public ConfigureMultiRegionalDbContext(ConfigurationExtension configuration, DbContextManager<T> dbContext)
2019-12-16 14:55:59 +00:00
{
Configuration = configuration;
DbContext = dbContext;
}
public void Configure(string name, MultiRegionalDbContext<T> context)
{
context.Context = new System.Collections.Generic.List<T>();
const StringComparison cmp = StringComparison.InvariantCultureIgnoreCase;
foreach (var c in Configuration.GetConnectionStrings().Where(r =>
r.Name.Equals(name, cmp) || r.Name.StartsWith(name + ".", cmp) ||
r.Name.Equals(baseName, cmp) || r.Name.StartsWith(baseName + ".", cmp)
))
{
context.Context.Add(DbContext.Get(c.Name));
}
}
public void Configure(MultiRegionalDbContext<T> context)
{
Configure(baseName, context);
}
}
2019-11-29 12:26:53 +00:00
}