コード例 #1
0
 /// <summary>
 /// 获取上下文
 /// </summary>
 /// <returns></returns>
 public static IDbProvider GetContext(WriteAndRead type)
 {
     if (type == WriteAndRead.Read)
     {
         return(new ReadSqlServerProvider(OpenConfiguration.Configuration["ConnectionStrings:ReadSqlServerConnection"].ToString()));
     }
     else
     {
         return(new WriteSqlServerProvider(OpenConfiguration.Configuration["ConnectionStrings:WriteSqlServerConnection"].ToString()));
     }
 }
コード例 #2
0
 /// <summary>
 /// 获取上下文
 /// </summary>
 /// <returns></returns>
 public static DbContext GetContext(WriteAndRead type)
 {
     if (type == WriteAndRead.Read)
     {
         //return new ReadSqlServerContext(new DbContextOptions<ReadSqlServerContext>());
         return(HttpContext.Current.RequestServices.GetService(typeof(ReadSqlServerContext)) as ReadSqlServerContext);
     }
     else
     {
         //return new WriteSqlServerContext(new DbContextOptions<WriteSqlServerContext>());
         return(HttpContext.Current.RequestServices.GetService(typeof(WriteSqlServerContext)) as WriteSqlServerContext);
     }
 }
コード例 #3
0
        /// <summary>
        /// 根据主键获取实体
        /// </summary>
        /// <param name="id">实体主键</param>
        /// <returns></returns>
        public TEntity GetById(int id, WriteAndRead writeAndRead = WriteAndRead.Read)
        {
            IDbProvider _provider;

            if (writeAndRead == WriteAndRead.Read)
            {
                _provider = _readDbContext;
            }
            else
            {
                _provider = _WriteDbContext;
            }
            return(_provider.GetById <TEntity>(id));
        }
コード例 #4
0
        public IDbConnection GetConnection(WriteAndRead writeAndRead)
        {
            switch (writeAndRead)
            {
            case WriteAndRead.Write:
                _Connection.ConnectionString = _Options.WriteConnection;
                break;

            case WriteAndRead.Read:
                _Connection.ConnectionString = QueryStategy();
                break;
            }
            return(_Connection);
        }
コード例 #5
0
        /// <summary>
        /// 根据lambda表达式条件获取单个实体
        /// </summary>
        /// <param name="predicate">lambda表达式条件</param>
        /// <returns></returns>
        public TEntity GetItemByQuery(Expression <Func <TEntity, bool> > predicate, WriteAndRead writeAndRead = WriteAndRead.Read)
        {
            TeacherClubContext context;

            if (writeAndRead == WriteAndRead.Read)
            {
                context = _readDbContext;
            }
            else
            {
                context = _WriteDbContext;
            }
            return(context.Set <TEntity>().SingleOrDefault(predicate));
        }
コード例 #6
0
        /// <summary>
        /// 根据lambda表达式条件获取单个实体
        /// </summary>
        /// <param name="predicate">lambda表达式条件</param>
        /// <returns></returns>
        public TEntity GetItemByQuery(Expression <Func <TEntity, bool> > predicate, WriteAndRead writeAndRead = WriteAndRead.Read)
        {
            IDbProvider _provider;

            if (writeAndRead == WriteAndRead.Read)
            {
                _provider = _readDbContext;
            }
            else
            {
                _provider = _WriteDbContext;
            }
            return(_provider.GetItemByQuery <TEntity>(predicate));
        }
コード例 #7
0
        /// <summary>
        /// 执行Sql查询语句,返回受影响行数
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="sqlParameters"></param>
        /// <returns></returns>
        public int ExecuteSql(string sql, SqlParameter[] sqlParameters = null, WriteAndRead writeAndRead = WriteAndRead.Read)
        {
            IDbProvider _provider;

            if (writeAndRead == WriteAndRead.Read)
            {
                _provider = _readDbContext;
            }
            else
            {
                _provider = _WriteDbContext;
            }

            return(_provider.ExecuteSql(sql, sqlParameters));
        }
コード例 #8
0
        /// <summary>
        /// 根据主键获取实体
        /// </summary>
        /// <param name="id">实体主键</param>
        /// <returns></returns>
        public TEntity GetById(int id, WriteAndRead writeAndRead = WriteAndRead.Read)
        {
            int a = writeAndRead == WriteAndRead.Read ? 0 : 1;
            TeacherClubContext context;

            if (writeAndRead == WriteAndRead.Read)
            {
                context = _readDbContext;
            }
            else
            {
                context = _WriteDbContext;
            }
            return(context.Set <TEntity>().Find(id));
        }
コード例 #9
0
        /// <summary>
        /// 执行Sql查询语句,返回受影响行数
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="sqlParameters"></param>
        /// <returns></returns>
        public int ExecuteSql(string sql, SqlParameter[] sqlParameters = null, WriteAndRead writeAndRead = WriteAndRead.Read)
        {
            TeacherClubContext context;

            if (writeAndRead == WriteAndRead.Read)
            {
                context = _readDbContext;
            }
            else
            {
                context = _WriteDbContext;
            }
            if (sqlParameters != null)
            {
                return(context.Database.ExecuteSqlCommand(sql, sqlParameters));
            }
            else
            {
                return(context.Database.ExecuteSqlCommand(sql));
            }
        }
コード例 #10
0
ファイル: LogicBase.cs プロジェクト: gaojiandonggg/gaojdclub
 /// <summary>
 /// 根据主键获取实体
 /// </summary>
 /// <param name="id">实体主键</param>
 /// <returns></returns>
 public TEntity GetById(int id, WriteAndRead writeAndRead = WriteAndRead.Read)
 {
     return(_dalBase.GetById(id));
 }
コード例 #11
0
ファイル: LogicBase.cs プロジェクト: gaojiandonggg/gaojdclub
 /// <summary>
 /// 根据lambda表达式条件获取单个实体
 /// </summary>
 /// <param name="predicate">lambda表达式条件</param>
 /// <returns></returns>
 public TEntity GetItemByQuery(Expression <Func <TEntity, bool> > predicate, WriteAndRead writeAndRead = WriteAndRead.Read)
 {
     return(_dalBase.GetItemByQuery(predicate));
 }
コード例 #12
0
ファイル: LogicBase.cs プロジェクト: gaojiandonggg/gaojdclub
 /// <summary>
 /// 执行存储过程,返回受影响行数
 /// </summary>
 /// <param name="sql"></param>
 /// <param name="sqlParameters"></param>
 /// <returns></returns>
 public int ExecuteStoredProcedure(string sql, SqlParameter[] sqlParameters = null, WriteAndRead writeAndRead = WriteAndRead.Read)
 {
     return(_dalBase.ExecuteStoredProcedure(sql, sqlParameters));
 }
コード例 #13
0
ファイル: LogicBase.cs プロジェクト: gaojiandonggg/gaojdclub
 /// <summary>
 /// 执行存储过程,返回实体
 /// </summary>
 /// <param name="sql"></param>
 /// <param name="sqlParameters"></param>
 /// <returns></returns>
 public List <TEntity> ExecuteStoredProcedureQuery(string sql, SqlParameter[] sqlParameters = null, WriteAndRead writeAndRead = WriteAndRead.Read)
 {
     return(_dalBase.ExecuteStoredProcedureQuery(sql, sqlParameters));
 }
コード例 #14
0
ファイル: CallContext.cs プロジェクト: Ljyi/eaven
 public static void SetData(WriteAndRead name, DbContext data) => state.GetOrAdd(name.ToString(), _ => new AsyncLocal <DbContext>()).Value = data;
コード例 #15
0
ファイル: CallContext.cs プロジェクト: Ljyi/eaven
 public static DbContext GetData(WriteAndRead name) => state.TryGetValue(name.ToString(), out AsyncLocal <DbContext> data) ? data.Value : null;
コード例 #16
0
        /// <summary>
        /// 执行存储过程,返回实体
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="sqlParameters"></param>
        /// <returns></returns>
        public List <TEntity> ExecuteStoredProcedureQuery(string sql, SqlParameter[] sqlParameters = null, WriteAndRead writeAndRead = WriteAndRead.Read)
        {
            TeacherClubContext context;

            if (writeAndRead == WriteAndRead.Read)
            {
                context = _readDbContext;
            }
            else
            {
                context = _WriteDbContext;
            }
            StringBuilder paraNames = new StringBuilder();

            if (sqlParameters != null)
            {
                foreach (var sqlPara in sqlParameters)
                {
                    paraNames.Append($" {sqlPara},");
                }
            }

            sql = paraNames.Length > 0 ?
                  $"exec {sql} {paraNames.ToString().Trim(',')}" : $"exec {sql} ";
            if (sqlParameters != null)
            {
                return(context.Set <TEntity>().FromSql <TEntity>(sql, sqlParameters).ToList());
            }
            else
            {
                return(context.Set <TEntity>().FromSql <TEntity>(sql).ToList());
            }
        }
コード例 #17
0
ファイル: DataDbContext.cs プロジェクト: Ljyi/eaven
 public DataDbContext(WriteAndRead writeRead)
 {
 }
コード例 #18
0
        /// <summary>
        /// 执行Sql查询语句,返回实体
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="sqlParameters"></param>
        /// <returns></returns>
        public List <TEntity> ExecuteSqlQuery(string sql, SqlParameter[] sqlParameters = null, WriteAndRead writeAndRead = WriteAndRead.Read)

        {
            TeacherClubContext context;

            if (writeAndRead == WriteAndRead.Read)
            {
                context = _readDbContext;
            }
            else
            {
                context = _WriteDbContext;
            }
            if (sqlParameters != null)
            {
                return(context.Set <TEntity>().FromSql <TEntity>(sql, sqlParameters).ToList());
            }
            else
            {
                return(context.Set <TEntity>().FromSql <TEntity>(sql).ToList());
            }
        }
コード例 #19
0
        /// <summary>
        /// 执行存储过程,返回受影响行数
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="sqlParameters"></param>
        /// <returns></returns>
        public int ExecuteStoredProcedure(string sql, SqlParameter[] sqlParameters = null, WriteAndRead writeAndRead = WriteAndRead.Read)
        {
            TeacherClubContext context;

            if (writeAndRead == WriteAndRead.Read)
            {
                context = _readDbContext;
            }
            else
            {
                context = _WriteDbContext;
            }
            StringBuilder paraNames = new StringBuilder();

            if (sqlParameters != null)
            {
                foreach (var sqlPara in sqlParameters)
                {
                    paraNames.Append($" {sqlPara},");
                }
            }

            sql = paraNames.Length > 0 ?
                  $"exec {sql} {paraNames.ToString().Trim(',')}" : $"exec {sql} ";

            if (sqlParameters != null)
            {
                return(context.Database.ExecuteSqlCommand(sql, sqlParameters));
            }
            else
            {
                return(context.Database.ExecuteSqlCommand(sql));
            }
        }
コード例 #20
0
        /// <summary>
        /// 获取上下文操作
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="OpertionType"></param>
        /// <returns></returns>
        public static IDbProvider CallContext <TEntity>(WriteAndRead type) where TEntity : class
        {
            var DbContext = GetContext(type);

            return((IDbProvider)DbContext);
        }
コード例 #21
0
        /// <summary>
        /// 获取上下文操作
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="OpertionType"></param>
        /// <returns></returns>
        public static TEntity CallContext <TEntity>(WriteAndRead type) where TEntity : DbContext
        {
            var DbContext = GetContext(type);

            return((TEntity)DbContext);
        }
コード例 #22
0
        /// <summary>
        /// 执行存储过程,返回实体
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="sqlParameters"></param>
        /// <returns></returns>
        public List <TEntity> ExecuteStoredProcedureQuery(string sql, SqlParameter[] sqlParameters = null, WriteAndRead writeAndRead = WriteAndRead.Read)
        {
            IDbProvider _provider;

            if (writeAndRead == WriteAndRead.Read)
            {
                _provider = _readDbContext;
            }
            else
            {
                _provider = _WriteDbContext;
            }
            return(_provider.ExecuteStoredProcedureQuery <TEntity>(sql, sqlParameters));
        }