108 lines
2.9 KiB
C#
108 lines
2.9 KiB
C#
|
|
using Cowain.Base.Helpers;
|
|||
|
|
using Cowain.Base.Models;
|
|||
|
|
using Microsoft.EntityFrameworkCore;
|
|||
|
|
using Microsoft.Extensions.Configuration;
|
|||
|
|
using Microsoft.Extensions.Options;
|
|||
|
|
using System.Reflection;
|
|||
|
|
|
|||
|
|
namespace Cowain.Base.DBContext;
|
|||
|
|
|
|||
|
|
public class SqlDbContext : DbContext
|
|||
|
|
{
|
|||
|
|
private List<Assembly>? _assemblies;
|
|||
|
|
/// <summary>
|
|||
|
|
/// dbcontext
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="options">options</param>
|
|||
|
|
public SqlDbContext(DbContextOptions options) : base(options)
|
|||
|
|
{
|
|||
|
|
_assemblies = [.. GetAssembliesFromPluginDirectory()];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
protected override void OnConfiguring(DbContextOptionsBuilder options)
|
|||
|
|
{
|
|||
|
|
base.OnConfiguring(options);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
override protected void OnModelCreating(ModelBuilder modelBuilder)
|
|||
|
|
{
|
|||
|
|
MapModelEntities(modelBuilder);
|
|||
|
|
base.OnModelCreating(modelBuilder);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// over write EnsureCreated
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public virtual bool EnsureCreated()
|
|||
|
|
{
|
|||
|
|
return Database.EnsureCreated();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// over write EnsureCreated
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public async Task<bool> EnsureCreatedAsync()
|
|||
|
|
{
|
|||
|
|
return await Database.EnsureCreatedAsync();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Auto Mapping Entity
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="modelBuilder">ModelBuilder</param>
|
|||
|
|
private void MapModelEntities(ModelBuilder modelBuilder)
|
|||
|
|
{
|
|||
|
|
var baseModelType = typeof(BaseModel);
|
|||
|
|
var entityTypes = _assemblies?
|
|||
|
|
.SelectMany(assembly => assembly.GetTypes())
|
|||
|
|
.Where(t => t.IsClass && !t.IsAbstract && t.IsSubclassOf(baseModelType));
|
|||
|
|
if (entityTypes == null)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
foreach (var type in entityTypes)
|
|||
|
|
{
|
|||
|
|
modelBuilder.Model.AddEntityType(type);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// Get all assemblies including those in the plugin directory
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns>IEnumerable<Assembly></returns>
|
|||
|
|
private IEnumerable<Assembly> GetAssembliesFromPluginDirectory()
|
|||
|
|
{
|
|||
|
|
var assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
|
|||
|
|
var pluginPath = Path.Combine(AppContext.BaseDirectory, "Plugins");
|
|||
|
|
if (Directory.Exists(pluginPath))
|
|||
|
|
{
|
|||
|
|
var pluginAssemblies = Directory.GetFiles(pluginPath, "Plugin.*.dll")
|
|||
|
|
.Select(Assembly.LoadFrom);
|
|||
|
|
assemblies.AddRange(pluginAssemblies);
|
|||
|
|
}
|
|||
|
|
return assemblies;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// create DbSet
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T">Entity</typeparam>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public virtual DbSet<T> GetDbSet<T>() where T : class
|
|||
|
|
{
|
|||
|
|
if (Model.FindEntityType(typeof(T)) != null)
|
|||
|
|
{
|
|||
|
|
return Set<T>();
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
throw new Exception($"type {typeof(T).Name} is not add into DbContext ");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|