100 lines
3.3 KiB
C#
100 lines
3.3 KiB
C#
|
|
using Microsoft.Extensions.Configuration;
|
||
|
|
using QYZH.InteractiveMagazine.Models.Entity;
|
||
|
|
using SqlSugar;
|
||
|
|
|
||
|
|
namespace QYZH.InteractiveMagazine.Repository;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// SqlSugar 数据库上下文封装(静态类)
|
||
|
|
/// </summary>
|
||
|
|
public static class SqlSugarDbContext
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// SqlSugarClient 实例
|
||
|
|
/// </summary>
|
||
|
|
private static SqlSugarClient? _db;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 配置对象
|
||
|
|
/// </summary>
|
||
|
|
private static IConfiguration? _configuration;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 初始化数据库上下文(在应用启动时调用一次)
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="configuration">配置对象</param>
|
||
|
|
public static void Init(IConfiguration configuration)
|
||
|
|
{
|
||
|
|
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 获取 SqlSugarClient 实例
|
||
|
|
/// </summary>
|
||
|
|
/// <returns>SqlSugarClient 实例</returns>
|
||
|
|
public static SqlSugarClient GetDb()
|
||
|
|
{
|
||
|
|
if (_configuration == null)
|
||
|
|
{
|
||
|
|
throw new InvalidOperationException("请先调用 SqlSugarDbContext.Init(configuration) 进行初始化");
|
||
|
|
}
|
||
|
|
|
||
|
|
if (_db == null)
|
||
|
|
{
|
||
|
|
lock (typeof(SqlSugarDbContext))
|
||
|
|
{
|
||
|
|
if (_db == null)
|
||
|
|
{
|
||
|
|
var connectionString = _configuration.GetConnectionString("DefaultConnection");
|
||
|
|
if (string.IsNullOrWhiteSpace(connectionString))
|
||
|
|
{
|
||
|
|
throw new InvalidOperationException("未找到连接字符串 DefaultConnection");
|
||
|
|
}
|
||
|
|
|
||
|
|
_db = new SqlSugarClient(new ConnectionConfig
|
||
|
|
{
|
||
|
|
ConnectionString = connectionString,
|
||
|
|
DbType = DbType.MySql,
|
||
|
|
IsAutoCloseConnection = true,
|
||
|
|
InitKeyType = InitKeyType.Attribute
|
||
|
|
},
|
||
|
|
db =>
|
||
|
|
{
|
||
|
|
// 配置软删除全局过滤(继承 BaseEntity 的实体都有效)
|
||
|
|
db.QueryFilter.AddTableFilter<BaseEntity>(it => it.IsDeleted == false);
|
||
|
|
|
||
|
|
// 开启日志打印
|
||
|
|
db.Aop.OnLogExecuting = (sql, pars) =>
|
||
|
|
{
|
||
|
|
Console.WriteLine($"[SQL执行] {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
|
||
|
|
Console.WriteLine($"[SQL语句] {sql}");
|
||
|
|
Console.WriteLine($"[SQL参数] {string.Join(", ", pars.Select(p => $"{p.ParameterName}={p.Value}"))}");
|
||
|
|
Console.WriteLine(new string('-', 50));
|
||
|
|
};
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return _db;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 代码优先初始化(可选)
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="entityTypes">实体类型数组</param>
|
||
|
|
public static void InitializeCodeFirst(params Type[] entityTypes)
|
||
|
|
{
|
||
|
|
var db = GetDb();
|
||
|
|
|
||
|
|
// 创建数据库(如果不存在)
|
||
|
|
db.DbMaintenance.CreateDatabase();
|
||
|
|
|
||
|
|
// 初始化表结构
|
||
|
|
if (entityTypes != null && entityTypes.Length > 0)
|
||
|
|
{
|
||
|
|
db.CodeFirst.InitTables(entityTypes);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|