コード例 #1
0
        /// <summary>
        /// 添加数据上下文
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="context">数据上下文对象</param>
        public void Add(string connectionString, DbContextBase context)
        {
            DbContextGroup group = _groups.GetOrAdd(connectionString, () => new DbContextGroup());

            group.DbContexts.AddIfNotExist(context);
            context.ContextGroup      = group;
            _groups[connectionString] = group;
        }
コード例 #2
0
        /// <summary>
        /// 对数据库连接开启事务
        /// </summary>
        /// <param name="connection">数据库连接对象</param>
        public void BeginOrUseTransaction(DbConnection connection)
        {
            DbContextGroup group = _groups.GetOrDefault(connection.ConnectionString);

            if (group == null)
            {
                throw new OsharpException("开启事务时,指定连接对象的上下文组无法找到");
            }
            group.BeginOrUseTransaction(connection);
        }
コード例 #3
0
        /// <summary>
        /// 对数据库连接开启事务
        /// </summary>
        /// <param name="connection">数据库连接对象</param>
        /// <param name="cancellationToken">异步取消标记</param>
        /// <returns></returns>
        public Task BeginOrUseTransactionAsync(DbConnection connection, CancellationToken cancellationToken = default(CancellationToken))
        {
            DbContextGroup group = _groups.GetOrDefault(connection.ConnectionString);

            if (group == null)
            {
                throw new OsharpException("开启事务时,指定连接对象的上下文组无法找到");
            }
            return(group.BeginOrUseTransactionAsync(connection, cancellationToken));
        }
コード例 #4
0
        /// <summary>
        /// 获取指定类型的数据上下文
        /// </summary>
        /// <param name="contextType">数据上下文类型</param>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <returns>数据上下文对象</returns>
        public DbContextBase Get(Type contextType, string connectionString = null)
        {
            if (connectionString == null)
            {
                return(_groups.Values.SelectMany(m => m.DbContexts).FirstOrDefault(m => m.GetType() == contextType));
            }
            DbContextGroup group = _groups.GetOrDefault(connectionString);

            return(@group?.DbContexts.FirstOrDefault(m => m.GetType() == contextType));
        }
コード例 #5
0
        /// <summary>
        /// 移除指定类型的数据上下文
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="contextType">数据上下文类型</param>
        public void Remove(string connectionString, Type contextType)
        {
            DbContextGroup group   = _groups.GetOrDefault(connectionString);
            DbContextBase  context = group?.DbContexts.FirstOrDefault(m => m.GetType() == contextType);

            if (context == null)
            {
                return;
            }
            group.DbContexts.Remove(context);
            context.ContextGroup = null;
            if (group.DbContexts.Count == 0)
            {
                _groups.TryRemove(connectionString, out group);
                return;
            }
            _groups[connectionString] = group;
        }
コード例 #6
0
        /// <summary>
        /// 获取指定类型的数据上下文
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="contextType">数据上下文类型</param>
        /// <returns>数据上下文对象</returns>
        public DbContextBase Get(string connectionString, Type contextType)
        {
            DbContextGroup group = _groups.GetOrDefault(connectionString);

            return(group?.DbContexts.FirstOrDefault(m => m.GetType() == contextType));
        }