BaseRepository中使用异步事务报错 返回

SqlSugar
2 46
该叫什么 ethan 发布于3天前
悬赏:100 飞吻

//构造函数

public BaseRepository(ISqlSugarClient client = null)

        {

            var configId = typeof(T).GetCustomAttribute<TenantAttribute>()?.configId;

            if (configId != null)

            {

                Context = DbScoped.SugarScope.GetConnectionScope(configId);

            }

            else

            {

                Context = client ?? DbScoped.SugarScope.GetConnectionScope(0);//根据类传入的ConfigId自动选择

            }

        }

  

//事务

public async Task<DbResult<bool>> UseTran(Action action)

        {

           return  await Context.Ado.UseTranAsync(() => { action(); return Task.CompletedTask; });

        }


 /// <summary>

        /// 多租户事务

        /// </summary>

        /// <param name="action"></param>

        /// <returns></returns>

        public async Task<DbResult<bool>> UseTranTenant(Action action)

        {

            return  await DbScoped.Sugar.UseTranAsync(() => { action(); return Task.CompletedTask; });

        }


//service中调用

DbResult<bool> dbResult =await baseRepository.UseTran(() =>{

//具体操作

}):


异常信息:The connection does not support MultipleActiveResultSets.

热忱回答2

  • public async Task<DbResult<bool>> UseTran(Func<Task> action)

            {

               return  await Context.Ado.UseTranAsync(async () => { await action()  });

            }

    这样是正确用法

    0 回复
  • ethan ethan VIP0
    2天前

    @fate sta:谢谢大神

    0 回复