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);
        }