Esempio n. 1
0
        /// <summary>
        /// 获取一个DbContext对象
        /// </summary>
        /// <param name="name">连接名称</param>
        /// <param name="autoClose">是否自动关闭</param>
        /// <returns>DbContext对象</returns>
        public static DbContext GetContext(string name, bool autoClose)
        {
            //得到映射后连接名称
            name = DbConnectionMappingContext.GetMappedConnectionName(name);

            DbProviderFactory factory = DbConnectionManager.GetDbProviderFactory(name);

            DbConnectionStringBuilder csb = factory.CreateConnectionStringBuilder();

            csb.ConnectionString = DbConnectionManager.GetConnectionString(name);

            bool enlist = true;

            if (csb.ContainsKey("enlist"))
            {
                enlist = (bool)csb["enlist"];
            }

            DbContext result = null;

            if (enlist)
            {
                result = new AutoEnlistDbContext(name, autoClose);
            }
            else
            {
                result = new NotEnlistDbContext(name, autoClose);
            }

            result.InitDbContext(name, autoClose);

            result._autoClose      = autoClose;
            result._commandTimeout = DbConnectionManager.GetCommandTimeout(name);

            return(result);
        }
Esempio n. 2
0
        private async Task <Tuple <DbConnection, bool> > CreateConnectionAsync(string name)
        {
            bool isConnectionCreator = false;

            // non-transactional operation
            GraphWithoutTransaction connections = this.GraphWithoutTx;

            DbConnection connection = GetConnection(name);

            if ((connection != null) && (connection.State != ConnectionState.Open))
            {
                if (connection.ConnectionString.IsNullOrEmpty())
                {
                    connection.ConnectionString = DbConnectionManager.GetConnectionString(name);
                }

                await OpenConnectionAsync(name, connection);

                WriteTraceInfo(connection.DataSource + "." + connection.Database
                               + "[" + SNTPClient.AdjustedTime.ToString("yyyyMMdd HH:mm:ss.fff") + "]", " Open Connection ");
            }

            return(new Tuple <DbConnection, bool>(connection, isConnectionCreator));
        }