sqlSugarCore CallContext不允许访问使用了,怎么办 返回

SqlSugar 老数据
6 2396

image.png

热忱回答6

  • 这个是私有类,底层就是

    https://github.com/sunkaixuan/SqlSugar/blob/dev/Src/Asp.Net/SqlSugar/Utilities/CallContext.cs

    0 回复
  • 用我现成的功能就好了

    SqlSugarClient db = new SqlSugarClient(
                    new ConnectionConfig() {
                        ConnectionString = Config.ConnectionString,
                        DbType = DbType.SqlServer,
                        IsAutoCloseConnection = true,
                        IsShardSameThread= true //设为true相同线程是同一个SqlConnection
                    });
               
    return db;


    0 回复
  • image.png

    @fate stay night:我想通过不同的构造函数初始化不同的连接,怎么写?

    0 回复
  • 把我这个类自个建一个 https://github.com/sunkaixuan/SqlSugar/blob/dev/Src/Asp.Net/SqlSugar/Utilities/CallContext.cs

    0 回复
  • public static class CallContext

        {

            static ConcurrentDictionary<string, AsyncLocal<object>> state = new ConcurrentDictionary<string, AsyncLocal<object>>();

            

            public static void SetData(string name, object data) =>

                state.GetOrAdd(name, _ => new AsyncLocal<object>()).Value = data;

            

            public static object GetData(string name) =>

                state.TryGetValue(name, out AsyncLocal<object> data) ? data.Value : null;

        }

    @fate stay night

    0 回复
  • @fate stay night:网上的解决方案,这样可以吗

    0 回复