Initial_commit_SecMPS_v2
This commit is contained in:
160
api_sqlsugar/VolPro.Core/EFDbContext/BaseDbContext.cs
Normal file
160
api_sqlsugar/VolPro.Core/EFDbContext/BaseDbContext.cs
Normal file
@@ -0,0 +1,160 @@
|
||||
using VolPro.Core.Configuration;
|
||||
using VolPro.Core.Extensions;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyModel;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
using VolPro.Core.Const;
|
||||
using VolPro.Core.Enums;
|
||||
using VolPro.Core.BaseProvider;
|
||||
using VolPro.Core.DbSqlSugar;
|
||||
using SqlSugar;
|
||||
|
||||
namespace VolPro.Core.EFDbContext
|
||||
{
|
||||
public abstract class BaseDbContext : DbContext
|
||||
{
|
||||
|
||||
|
||||
public ISqlSugarClient SqlSugarClient { get; set; }
|
||||
|
||||
public bool QueryTracking
|
||||
{
|
||||
set
|
||||
{
|
||||
}
|
||||
}
|
||||
public BaseDbContext():base() { }
|
||||
|
||||
public ISugarQueryable<TEntity> Set<TEntity>(bool filterDeleted=false) where TEntity : class, new()
|
||||
{
|
||||
return SqlSugarClient.Set<TEntity>(filterDeleted);
|
||||
}
|
||||
|
||||
public int SaveChanges()
|
||||
{
|
||||
return SqlSugarClient.SaveQueues();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
protected void UseDbType(DbContextOptionsBuilder optionsBuilder, string connectionString)
|
||||
{
|
||||
//if (DBType.Name == DbCurrentType.MsSql.ToString())
|
||||
//{
|
||||
// if (AppSetting.UseSqlserver2008)
|
||||
// {
|
||||
// optionsBuilder.ReplaceService<IQueryTranslationPostprocessorFactory, SqlServer2008QueryTranslationPostprocessorFactory>();
|
||||
// }
|
||||
// optionsBuilder.UseSqlServer(connectionString);
|
||||
//}
|
||||
//else if (DBType.Name == DbCurrentType.MySql.ToString())
|
||||
//{
|
||||
// optionsBuilder.UseMySql(connectionString, new MySqlServerVersion(new Version(8, 0, 11)));
|
||||
//}
|
||||
//else if (DBType.Name == DbCurrentType.PgSql.ToString())
|
||||
//{
|
||||
// optionsBuilder.UseNpgsql(connectionString);
|
||||
//}
|
||||
//else if (DBType.Name == DbCurrentType.Oracle.ToString())
|
||||
//{
|
||||
// optionsBuilder.UseOracle(connectionString, b => b.UseOracleSQLCompatibility("11"));
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// throw new Exception("数据库未实现");
|
||||
// // optionsBuilder.UseSqlServer(connectionString);
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
//protected void OnModelCreating(ModelBuilder modelBuilder, Type type)
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// //获取所有类库
|
||||
// var compilationLibrary = DependencyContext
|
||||
// .Default
|
||||
// .CompileLibraries
|
||||
// .Where(x => !x.Serviceable && x.Type != "package" && x.Type == "project");
|
||||
// foreach (var _compilation in compilationLibrary)
|
||||
// {
|
||||
// //加载指定类
|
||||
// AssemblyLoadContext.Default
|
||||
// .LoadFromAssemblyName(new AssemblyName(_compilation.Name))
|
||||
// .GetTypes().Where(x => x.GetTypeInfo().BaseType != null
|
||||
// && x.BaseType == (type)).ToList()
|
||||
// .ForEach(t => { modelBuilder.Entity(t); });
|
||||
// }
|
||||
|
||||
// //Oracle数据库指定表名与列名全部大写
|
||||
// if (DBType.Name == DbCurrentType.Oracle.ToString())
|
||||
// {
|
||||
// foreach (var entity in modelBuilder.Model.GetEntityTypes())
|
||||
// {
|
||||
// string tableName = entity.GetTableName().ToUpper();
|
||||
// if (tableName.StartsWith("SYS_") || tableName.StartsWith("DEMO_"))
|
||||
// {
|
||||
// entity.SetTableName(entity.GetTableName().ToUpper());
|
||||
// foreach (var property in entity.GetProperties())
|
||||
// {
|
||||
// property.SetColumnName(property.Name.ToUpper());
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// //重置系统表名小写,如果是mysql数据库,创建的表名都是小写的,请取消此注释
|
||||
// //foreach (var entity in modelBuilder.Model.GetEntityTypes())
|
||||
// //{
|
||||
// //
|
||||
// // if (entity.GetTableName().StartsWith("Sys_"))
|
||||
// // {
|
||||
// // Console.WriteLine(entity.GetTableName());
|
||||
// // entity.SetTableName(entity.GetTableName().ToLower());
|
||||
// // }
|
||||
// // //// 重置所有列名
|
||||
// // //foreach (var property in entity.GetProperties())
|
||||
// // //{
|
||||
// // // //StoreObjectIdentifier
|
||||
// // // property.SetColumnName(property.Name);
|
||||
// // //}
|
||||
// //}
|
||||
|
||||
// //插件式开发
|
||||
// //try
|
||||
// //{
|
||||
// // string rootPath = (AppSetting.CurrentPath + "\\plugs").ReplacePath();
|
||||
// // foreach (var item in Directory.GetFiles(rootPath).Where(x => x.EndsWith(".dll")))
|
||||
// // {
|
||||
// // string path = ($"{item}").ReplacePath();
|
||||
// // Assembly.LoadFile(path).GetTypes().Where(x => x.GetTypeInfo().BaseType != null
|
||||
// // && x.BaseType == (type)).ToList()
|
||||
// // .ForEach(t => {
|
||||
// // Console.Write(t.Name);
|
||||
// // modelBuilder.Entity(t);
|
||||
|
||||
// // });
|
||||
// // }
|
||||
// //}
|
||||
// //catch (Exception ex)
|
||||
// //{
|
||||
// // Console.WriteLine($"EF解析类库异常:{ex.Message + ex.StackTrace}");
|
||||
// //}
|
||||
|
||||
// base.OnModelCreating(modelBuilder);
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// string mapPath = ($"Log/").MapPath();
|
||||
// Utilities.FileHelper.WriteFile(mapPath, $"syslog_{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt", ex.Message + ex.StackTrace + ex.Source);
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
}
|
||||
}
|
||||
16
api_sqlsugar/VolPro.Core/EFDbContext/DbContext.cs
Normal file
16
api_sqlsugar/VolPro.Core/EFDbContext/DbContext.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace VolPro.Core.EFDbContext
|
||||
{
|
||||
public abstract class DbContext//: SqlSugarProvider
|
||||
{
|
||||
//public DbContext(ConnectionConfig connection):base(connection)
|
||||
//{
|
||||
//}
|
||||
}
|
||||
}
|
||||
34
api_sqlsugar/VolPro.Core/EFDbContext/EFLoggerProvider.cs
Normal file
34
api_sqlsugar/VolPro.Core/EFDbContext/EFLoggerProvider.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using VolPro.Core.Utilities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace VolPro.Core.EFDbContext
|
||||
{
|
||||
public class EFLoggerProvider : ILoggerProvider
|
||||
{
|
||||
public ILogger CreateLogger(string categoryName) => new EFLogger(categoryName);
|
||||
public void Dispose() { }
|
||||
}
|
||||
public class EFLogger : ILogger
|
||||
{
|
||||
private readonly string categoryName;
|
||||
|
||||
public EFLogger(string categoryName) => this.categoryName = categoryName;
|
||||
|
||||
public bool IsEnabled(LogLevel logLevel) => true;
|
||||
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
|
||||
{
|
||||
//ef core执行数据库查询时的categoryName为Microsoft.EntityFrameworkCore.Database.Command,日志级别为Information
|
||||
if (categoryName == "Microsoft.EntityFrameworkCore.Database.Command"
|
||||
&& logLevel == LogLevel.Information)
|
||||
{
|
||||
var logContent = formatter(state, exception);
|
||||
Console.WriteLine(logContent);
|
||||
}
|
||||
}
|
||||
public IDisposable BeginScope<TState>(TState state) => null;
|
||||
}
|
||||
}
|
||||
30
api_sqlsugar/VolPro.Core/EFDbContext/ServiceDbContext.cs
Normal file
30
api_sqlsugar/VolPro.Core/EFDbContext/ServiceDbContext.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using VolPro.Core.DBManager;
|
||||
using VolPro.Core.Extensions.AutofacManager;
|
||||
using VolPro.Entity.SystemModels;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using SqlSugar;
|
||||
using VolPro.Core.DbSqlSugar;
|
||||
|
||||
namespace VolPro.Core.EFDbContext
|
||||
{
|
||||
public class ServiceDbContext : BaseDbContext, IDependency
|
||||
{
|
||||
private string dbServiceId { get; set; }
|
||||
|
||||
public ServiceDbContext() : base()
|
||||
{
|
||||
this.dbServiceId = dbServiceId;
|
||||
base.SqlSugarClient = DbManger.ServiceDb;
|
||||
}
|
||||
|
||||
|
||||
public ServiceDbContext(string dbServiceId) : base()
|
||||
{
|
||||
this.dbServiceId = dbServiceId;
|
||||
base.SqlSugarClient = DbManger.ServiceDb;
|
||||
}
|
||||
}
|
||||
}
|
||||
19
api_sqlsugar/VolPro.Core/EFDbContext/SysDbContext.cs
Normal file
19
api_sqlsugar/VolPro.Core/EFDbContext/SysDbContext.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using VolPro.Core.Extensions.AutofacManager;
|
||||
using VolPro.Core.DBManager;
|
||||
using VolPro.Entity.SystemModels;
|
||||
using System.Reflection.Emit;
|
||||
using VolPro.Core.DbSqlSugar;
|
||||
|
||||
namespace VolPro.Core.EFDbContext
|
||||
{
|
||||
public class SysDbContext : BaseDbContext, IDependency
|
||||
{
|
||||
public SysDbContext() : base() {
|
||||
base.SqlSugarClient = DbManger.SysDbContext;
|
||||
}
|
||||
}
|
||||
}
|
||||
19
api_sqlsugar/VolPro.Core/EFDbContext/TestDbContext.cs
Normal file
19
api_sqlsugar/VolPro.Core/EFDbContext/TestDbContext.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using VolPro.Core.DBManager;
|
||||
using VolPro.Core.Extensions.AutofacManager;
|
||||
using VolPro.Entity.SystemModels;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using VolPro.Core.DbSqlSugar;
|
||||
|
||||
namespace VolPro.Core.EFDbContext
|
||||
{
|
||||
public class TestDbContext : BaseDbContext, IDependency
|
||||
{
|
||||
|
||||
public TestDbContext() : base() {
|
||||
base.SqlSugarClient = DbManger.GetConnection(nameof(TestDbContext));
|
||||
}
|
||||
}
|
||||
}
|
||||
19
api_sqlsugar/VolPro.Core/EFDbContext/自定义DbContext.cs
Normal file
19
api_sqlsugar/VolPro.Core/EFDbContext/自定义DbContext.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using VolPro.Core.DBManager;
|
||||
using VolPro.Core.Extensions.AutofacManager;
|
||||
using VolPro.Entity.SystemModels;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using VolPro.Core.DbSqlSugar;
|
||||
|
||||
namespace VolPro.Core.EFDbContext
|
||||
{
|
||||
public class 自定义DbContext : BaseDbContext, IDependency
|
||||
{
|
||||
public 自定义DbContext() : base()
|
||||
{
|
||||
base.SqlSugarClient = DbManger.GetConnection(nameof(自定义DbContext));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user