/// <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())); } }
/// <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); } }
/// <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)); }
public IDbConnection GetConnection(WriteAndRead writeAndRead) { switch (writeAndRead) { case WriteAndRead.Write: _Connection.ConnectionString = _Options.WriteConnection; break; case WriteAndRead.Read: _Connection.ConnectionString = QueryStategy(); break; } return(_Connection); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); }
/// <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)); } }
/// <summary> /// 根据主键获取实体 /// </summary> /// <param name="id">实体主键</param> /// <returns></returns> public TEntity GetById(int id, WriteAndRead writeAndRead = WriteAndRead.Read) { return(_dalBase.GetById(id)); }
/// <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)); }
/// <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)); }
/// <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)); }
public static void SetData(WriteAndRead name, DbContext data) => state.GetOrAdd(name.ToString(), _ => new AsyncLocal <DbContext>()).Value = data;
public static DbContext GetData(WriteAndRead name) => state.TryGetValue(name.ToString(), out AsyncLocal <DbContext> data) ? data.Value : null;
/// <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()); } }
public DataDbContext(WriteAndRead writeRead) { }
/// <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()); } }
/// <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)); } }
/// <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); }
/// <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); }
/// <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)); }