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

104 lines
3.0 KiB
C#
Raw Normal View History

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
{
Postrge,
MySql
}
2019-12-16 14:55:59 +00:00
public class BaseDbContext : DbContext
2019-11-29 12:26:53 +00:00
{
2020-08-27 21:45:07 +00:00
public string baseName;
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-09-29 17:13:09 +00:00
Database.Migrate();
2020-09-02 15:23:39 +00:00
}
2019-12-13 11:37:58 +00:00
2019-11-29 12:26:53 +00:00
internal ILoggerFactory LoggerFactory { get; set; }
internal ConnectionStringSettings ConnectionStringSettings { get; set; }
2020-09-02 15:23:39 +00:00
internal Provider Provider { get; set; }
2019-11-29 12:26:53 +00:00
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseLoggerFactory(LoggerFactory);
optionsBuilder.EnableSensitiveDataLogging();
2020-09-02 15:23:39 +00:00
switch (ConnectionStringSettings.ProviderName)
{
case "MySql.Data.MySqlClient":
Provider = Provider.MySql;
optionsBuilder.UseMySql(ConnectionStringSettings.ConnectionString);
break;
case "Npgsql":
Provider = Provider.Postrge;
optionsBuilder.UseNpgsql(ConnectionStringSettings.ConnectionString);
break;
}
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();
}
}
}
public async ValueTask DisposeAsync()
{
if (Context == null) return;
foreach (var c in Context)
{
if (c != null)
{
await c.DisposeAsync();
}
}
}
}
2019-11-29 12:26:53 +00:00
}