Пример #1
0
        /// <summary>
        /// 数据访问对象[基本方法]
        /// 按指定的类型
        /// </summary>
        /// <returns></returns>
        protected AbsDBExtend GetDBExtend()
        {
            AbsDBExtend db             = null;
            string      contextName    = GetType().Name;//同一线程调用只创建一次
            var         _useCRLContext = CallContext.GetData <bool>(Base.UseCRLContextFlagName);

            if (_useCRLContext)//对于数据库事务,只创建一个上下文
            {
                contextName = Base.CRLContextName;
            }
            db = CallContext.GetData <AbsDBExtend>(contextName);
            if (db != null)
            {
                return(db);
            }
            var dbContext2 = GetDbContext();

            if (_useCRLContext)//使用CRLContext,需由CRLContext来关闭数据连接
            {
                dbContext2.DBHelper.AutoCloseConn = false;
            }
            db = DBExtendFactory.CreateDBExtend(dbContext2);
            if (dbLocation.ShardingDataBase == null)
            {
                db.OnUpdateNotifyCacheServer = OnUpdateNotifyCacheServer;
            }
            CallContext.SetData(contextName, db);
            var allList = Base.GetCallDBContext();

            allList.Add(contextName);
            //CallContext.SetData(Base.AllDBExtendName, allList);
            return(db);
        }
Пример #2
0
        /// <summary>
        /// 数据访问对象[基本方法]
        /// 按指定的类型
        /// </summary>
        /// <returns></returns>
        protected AbsDBExtend GetDBExtend(bool cache = true)
        {
            AbsDBExtend db          = null;
            string      contextName = "DBExtend." + GetType().Name;//同一线程调用只创建一次

            if (cache)
            {
                var _BeginTransContext = CallContext.GetData <bool>("_BeginTransContext");
                if (_BeginTransContext)//对于数据库事务,只创建一个上下文
                {
                    contextName = "TransDbContext";
                }
                db = CallContext.GetData <AbsDBExtend>(contextName);
                if (db != null)
                {
                    return(db);
                }
            }
            var dbContext2 = GetDbContext(cache);

            db = DBExtendFactory.CreateDBExtend(dbContext2);
            if (dbLocation.ShardingDataBase == null)
            {
                db.OnUpdateNotifyCacheServer = OnUpdateNotifyCacheServer;
            }
            if (cache)
            {
                var allKey  = "AllDBExtend";
                var allList = Base.GetCallDBContext();
                CallContext.SetData(contextName, db);
                allList.Add(contextName);
                CallContext.SetData(allKey, allList);
            }
            return(db);
        }
Пример #3
0
        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Delete <T>(this T obj) where T : IModel, new()
        {
            obj.CheckNull("obj");
            var dbContext = getDbContext <T>(obj.ManageName);
            var db        = DBExtendFactory.CreateDBExtend(dbContext);

            return(db.Delete <T>(obj.GetpPrimaryKeyValue()));
        }
Пример #4
0
        /// <summary>
        /// 保存更改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Save <T>(this T obj) where T : IModel, new()
        {
            obj.CheckNull("obj");
            var dbContext = getDbContext <T>(obj.ManageName);
            var db        = DBExtendFactory.CreateDBExtend(dbContext);

            return(db.Update(obj));
        }
Пример #5
0
        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Delete <T>(this T obj) where T : IModel, new()
        {
            obj.DbContext.CheckNull("DbContext");
            //var dbContext = getDbContext<T>(obj.DbContext.DBLocation.ManageName);
            var db = DBExtendFactory.CreateDBExtend(obj.DbContext);

            return(db.Delete <T>(TypeCache.GetpPrimaryKeyValue(obj)));
        }
Пример #6
0
        internal AbsDBExtend copyDBExtend()
        {
            var helper     = SettingConfig.GetDbAccess(dbContext.DBLocation);
            var dbContext2 = new DbContext(helper, dbContext.DBLocation);

            dbContext2.ShardingMainDataIndex = dbContext.ShardingMainDataIndex;
            dbContext2.UseSharding           = dbContext.UseSharding;
            return(DBExtendFactory.CreateDBExtend(dbContext2));
        }
Пример #7
0
        internal AbsDBExtend copyDBExtend()
        {
            var helper     = DBConfigRegister.GetDBHelper(dbContext.DBLocation);
            var dbContext2 = new DbContextInner(helper, dbContext.DBLocation);

            //dbContext2.ShardingMainDataIndex = dbContext.ShardingMainDataIndex;
            dbContext2.UseSharding = dbContext.UseSharding;
            return(DBExtendFactory.CreateDBExtend(dbContext2));
        }
Пример #8
0
        /// <summary>
        /// 数据访问对象[基本方法]
        /// 按指定的类型
        /// </summary>
        /// <returns></returns>
        protected AbsDBExtend GetDbHelper(Type type = null)
        {
            var dbContext2 = GetDbContext();

            if (type != null)
            {
                dbLocation.ManageType = type;
            }
            var db = DBExtendFactory.CreateDBExtend(dbContext2);

            if (dbLocation.ShardingDataBase == null)
            {
                db.OnUpdateNotifyCacheServer = OnUpdateNotifyCacheServer;
            }
            return(db);
        }
Пример #9
0
        /// <summary>
        /// 数据访问对象[基本方法]
        /// 按指定的类型
        /// </summary>
        /// <returns></returns>
        protected AbsDBExtend GetDBExtend()
        {
            AbsDBExtend db             = null;
            string      contextName    = GetType().Name;//同一线程调用只创建一次
            var         _useCRLContext = CallContext.GetData <bool>(Base.UseCRLContextFlagName);

            if (_useCRLContext)//对于数据库事务,只创建一个上下文
            {
                //todo 由于内置缓存问题,参数不能一直变化,不然生成重复缓存和重复存储过程
                contextName = Base.CRLContextName;
                db          = CallContext.GetData <AbsDBExtend>(contextName);
                if (db != null)
                {
                    return(db);
                }
            }

            var dbContext2 = GetDbContext();

            if (_useCRLContext)//使用CRLContext,需由CRLContext来关闭数据连接
            {
                dbContext2.DBHelper.AutoCloseConn = false;
            }
            db = DBExtendFactory.CreateDBExtend(dbContext2);
            if (dbLocation.ShardingLocation == null)
            {
                db.OnUpdateNotifyCacheServer = OnUpdateNotifyCacheServer;
            }
            if (_useCRLContext)
            {
                CallContext.SetData(contextName, db);
            }
            //占用内存..
            var allList = Base.GetCallDBContext();

            allList.Add(contextName);
            return(db);
        }
Пример #10
0
        /// <summary>
        /// 按完整查询条件进行更新
        /// goup语法不支持
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="updateValue"></param>
        /// <returns></returns>
        public static int Update <T>(this LambdaQuery <T> query, ParameCollection updateValue) where T : IModel, new()
        {
            var db = DBExtendFactory.CreateDBExtend(query.__DbContext);

            return(db.Update(query, updateValue));
        }
Пример #11
0
        /// <summary>
        /// 按完整查询条件进行删除
        /// goup语法不支持
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <returns></returns>
        public static int Delete <T>(this LambdaQuery <T> query) where T : IModel, new()
        {
            var db = DBExtendFactory.CreateDBExtend(query.__DbContext);

            return(db.Delete(query));
        }