protected ShardingDbContext(ContextScope scope, DbTransactionContext dbTran) : base(dbTran.Connection)
        {
            Scope = scope;
            UseTransaction(dbTran); //设置Scope后才可调用

            CheckTableCreate(dbTran.ConnectionString);
        }
Example #2
0
        /// <summary>
        /// 创建事务中的Db,执行后Db会被释放(用了using)
        /// </summary>
        public TRes InTran <TRes>(long id, DbTransactionContext tran, Func <T, TRes> operateFunc)
        {
            if (_dbConfig.DbConstructor.NewDbInTran == null)
            {
                throw new Exception($"{typeof(T).Name}没有定义带事务参数的构造函数,请在RegDb方法中添加设置。");
            }

            using var db = _dbConfig.DbConstructor.NewDbInTran(Scope(DataIdBuilder.ParseDate(id)), tran);
            return(operateFunc(db));
        }
Example #3
0
 internal void UseTransaction(DbTransactionContext tran)
 {
     Database.UseTransaction(tran.Transaction.GetDbTransaction());
 }
Example #4
0
 /// <summary>
 /// ShardingDb不能直接调用,构造时Scope为空,DynamicModelCacheKeyFactory会报错。
 /// </summary>
 protected BaseDbContext(DbTransactionContext dbTran) : base(GetDbOpts(dbTran.Connection))
 {
     UseTransaction(dbTran);
 }