Files
Yi.Admin/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContextCreationContext.cs

56 lines
1.5 KiB
C#
Raw Normal View History

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;
/// <summary>
/// SqlSugar数据库上下文创建上下文
/// </summary>
2024-01-25 01:43:00 +08:00
public class SqlSugarDbContextCreationContext
{
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; }
/// <summary>
/// 连接字符串
/// </summary>
2024-01-25 01:43:00 +08:00
public string ConnectionString { get; }
/// <summary>
/// 现有数据库连接
/// </summary>
public DbConnection? ExistingConnection { get; internal set; }
2024-01-25 01:43:00 +08:00
/// <summary>
/// 构造函数
/// </summary>
public SqlSugarDbContextCreationContext(
string connectionStringName,
string connectionString)
2024-01-25 01:43:00 +08:00
{
ConnectionStringName = connectionStringName;
ConnectionString = connectionString;
}
/// <summary>
/// 使用指定的上下文
/// </summary>
2024-01-25 01:43:00 +08:00
public static IDisposable Use(SqlSugarDbContextCreationContext context)
{
var previousContext = Current;
CurrentContextHolder.Value = context;
return new DisposeAction(() => CurrentContextHolder.Value = previousContext);
2024-01-25 01:43:00 +08:00
}
}