Example #1
0
        /// <summary>
        /// Creates the entity connection with wrappers.
        /// </summary>
        /// <param name="entityConnectionString">The original entity connection string. This may also be a single word, e.g., "MyEntities", in which case it is translated into "name=MyEntities" and looked up in the application configuration.</param>
        /// <param name="wrapperProviders">List for wrapper providers.</param>
        /// <returns>EntityConnection object which is based on a chain of providers.</returns>
        public static EntityConnection CreateEntityConnectionWithWrappers(string entityConnectionString, params string[] wrapperProviders)
        {
            // If there is only a single word in the connection string, treat it as the value for Name.
            if (!entityConnectionString.Contains('='))
            {
                entityConnectionString = "name=" + entityConnectionString;
            }

            EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder(entityConnectionString);

            // if connection string is name=EntryName, look up entry in the config file and parse it
            if (!String.IsNullOrEmpty(ecsb.Name))
            {
                var connStr = ConfigurationManager.ConnectionStrings[ecsb.Name];
                if (connStr == null)
                {
                    throw new ArgumentException("Specified named connection string '" + ecsb.Name + "' was not found in the configuration file.");
                }

                ecsb.ConnectionString = connStr.ConnectionString;
            }

            var workspace       = metadataWorkspaceMemoizer.GetOrAdd(ecsb.ConnectionString, _ => CreateWrappedMetadataWorkspace(ecsb.Metadata, wrapperProviders));
            var storeConnection = DbProviderFactories.GetFactory(ecsb.Provider).CreateConnection();

            storeConnection.ConnectionString = ecsb.ProviderConnectionString;
            var newEntityConnection = new EntityConnection(workspace, DbConnectionWrapper.WrapConnection(storeConnection, wrapperProviders));

            return(newEntityConnection);
        }
        /// <summary>
        /// Gets the provider manifest token.
        /// </summary>
        /// <param name="connection">The database connection.</param>
        /// <returns>Provider Manfiest Token suitable for inclusion in SSDL file and connection string</returns>
        /// <remarks>
        /// The provider manifest token is created by concatenating wrapped provider invariant name and its
        /// token separated by semicolon, for example when wrapping SqlClient for SQL Server 2005 the provider
        /// manifest token will be "System.Data.SqlClient;2005"
        /// </remarks>
        protected override string GetDbProviderManifestToken(DbConnection connection)
        {
            DbConnectionWrapper wrapper           = (DbConnectionWrapper)connection;
            DbConnection        wrappedConnection = wrapper.WrappedConnection;
            DbProviderServices  services          = DbProviderServices.GetProviderServices(wrappedConnection);

            string token = wrapper.WrappedProviderInvariantName + ";" + services.GetProviderManifestToken(wrappedConnection);

            return(token);
        }
Example #3
0
        /// <summary>
        /// Wraps the connection.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <param name="wrapperProviderInvariantNames">The wrapper provider invariant names.</param>
        /// <returns>Wrapped connection.</returns>
        internal static DbConnection WrapConnection(DbConnection connection, params string[] wrapperProviderInvariantNames)
        {
            foreach (string invariantName in wrapperProviderInvariantNames)
            {
                DbProviderFactory factory   = DbProviderFactories.GetFactory(invariantName);
                var connectionWrapper       = factory.CreateConnection();
                DbConnectionWrapper wrapper = (DbConnectionWrapper)connectionWrapper;
                wrapper.WrappedConnection = connection;
                connection = connectionWrapper;
            }

            return(connection);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DbTransactionWrapper"/> class.
 /// </summary>
 /// <param name="wrappedTransaction">The wrapped transaction.</param>
 /// <param name="connection">The connection.</param>
 protected DbTransactionWrapper(DbTransaction wrappedTransaction, DbConnectionWrapper connection)
 {
     this.wrappedTransaction = wrappedTransaction;
     this.connectionWrapper = connection;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="DbTransactionWrapper"/> class.
 /// </summary>
 /// <param name="wrappedTransaction">The wrapped transaction.</param>
 /// <param name="connection">The connection.</param>
 protected DbTransactionWrapper(DbTransaction wrappedTransaction, DbConnectionWrapper connection)
 {
     this.wrappedTransaction = wrappedTransaction;
     this.connectionWrapper  = connection;
 }