2024-01-25 01:43:00 +08:00
|
|
|
using System.Data.Common;
|
|
|
|
|
using Volo.Abp;
|
2024-01-25 10:06:32 +08:00
|
|
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
2024-01-25 01:43:00 +08:00
|
|
|
|
|
|
|
|
namespace Yi.Framework.SqlSugarCore;
|
|
|
|
|
|
2025-02-23 03:06:06 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// SqlSugar数据库上下文创建上下文
|
|
|
|
|
/// </summary>
|
2024-01-25 01:43:00 +08:00
|
|
|
public class SqlSugarDbContextCreationContext
|
|
|
|
|
{
|
2025-02-23 03:06:06 +08:00
|
|
|
private static readonly AsyncLocal<SqlSugarDbContextCreationContext> CurrentContextHolder =
|
|
|
|
|
new AsyncLocal<SqlSugarDbContextCreationContext>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取当前上下文
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static SqlSugarDbContextCreationContext Current => CurrentContextHolder.Value!;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 连接字符串名称
|
|
|
|
|
/// </summary>
|
2024-01-25 01:43:00 +08:00
|
|
|
public string ConnectionStringName { get; }
|
|
|
|
|
|
2025-02-23 03:06:06 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 连接字符串
|
|
|
|
|
/// </summary>
|
2024-01-25 01:43:00 +08:00
|
|
|
public string ConnectionString { get; }
|
|
|
|
|
|
2025-02-23 03:06:06 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 现有数据库连接
|
|
|
|
|
/// </summary>
|
|
|
|
|
public DbConnection? ExistingConnection { get; internal set; }
|
2024-01-25 01:43:00 +08:00
|
|
|
|
2025-02-23 03:06:06 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 构造函数
|
|
|
|
|
/// </summary>
|
|
|
|
|
public SqlSugarDbContextCreationContext(
|
|
|
|
|
string connectionStringName,
|
|
|
|
|
string connectionString)
|
2024-01-25 01:43:00 +08:00
|
|
|
{
|
|
|
|
|
ConnectionStringName = connectionStringName;
|
|
|
|
|
ConnectionString = connectionString;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-23 03:06:06 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 使用指定的上下文
|
|
|
|
|
/// </summary>
|
2024-01-25 01:43:00 +08:00
|
|
|
public static IDisposable Use(SqlSugarDbContextCreationContext context)
|
|
|
|
|
{
|
2025-02-23 03:06:06 +08:00
|
|
|
var previousContext = Current;
|
|
|
|
|
CurrentContextHolder.Value = context;
|
|
|
|
|
return new DisposeAction(() => CurrentContextHolder.Value = previousContext);
|
2024-01-25 01:43:00 +08:00
|
|
|
}
|
|
|
|
|
}
|