首次提交:本地项目同步到Gitea
This commit is contained in:
13
Cowain.Base/DBContext/IDataSeeding.cs
Normal file
13
Cowain.Base/DBContext/IDataSeeding.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Cowain.Base.DBContext;
|
||||
|
||||
public interface IDataSeeding
|
||||
{
|
||||
void DataSeeding(ModelBuilder modelBuilder);
|
||||
}
|
||||
107
Cowain.Base/DBContext/SqlDbContext.cs
Normal file
107
Cowain.Base/DBContext/SqlDbContext.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
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 ");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user