Blame view

Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContextCreationContext.cs 1.5 KB
515fceeb   “wangming”   框架初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  using System.Data.Common;
  using Volo.Abp;
  using Yi.Framework.SqlSugarCore.Abstractions;
  
  namespace Yi.Framework.SqlSugarCore;
  
  /// <summary>
  /// SqlSugar数据库上下文创建上下文
  /// </summary>
  public class SqlSugarDbContextCreationContext
  {
      private static readonly AsyncLocal<SqlSugarDbContextCreationContext> CurrentContextHolder = 
          new AsyncLocal<SqlSugarDbContextCreationContext>();
  
      /// <summary>
      /// 获取当前上下文
      /// </summary>
      public static SqlSugarDbContextCreationContext Current => CurrentContextHolder.Value!;
  
      /// <summary>
      /// 连接字符串名称
      /// </summary>
      public string ConnectionStringName { get; }
  
      /// <summary>
      /// 连接字符串
      /// </summary>
      public string ConnectionString { get; }
  
      /// <summary>
      /// 现有数据库连接
      /// </summary>
      public DbConnection? ExistingConnection { get; internal set; }
  
      /// <summary>
      /// 构造函数
      /// </summary>
      public SqlSugarDbContextCreationContext(
          string connectionStringName,
          string connectionString)
      {
          ConnectionStringName = connectionStringName;
          ConnectionString = connectionString;
      }
  
      /// <summary>
      /// 使用指定的上下文
      /// </summary>
      public static IDisposable Use(SqlSugarDbContextCreationContext context)
      {
          var previousContext = Current;
          CurrentContextHolder.Value = context;
          return new DisposeAction(() => CurrentContextHolder.Value = previousContext);
      }
  }