예제 #1
0
        // <summary>
        // This constructor allows to skip the initialization code for testing purposes.
        // </summary>
        internal EntityConnection(
            MetadataWorkspace workspace,
            DbConnection connection,
            bool skipInitialization,
            bool entityConnectionOwnsStoreConnection)
        {
            if (!skipInitialization)
            {
                if (!workspace.IsItemCollectionAlreadyRegistered(DataSpace.CSpace))
                {
                    throw new ArgumentException(Strings.EntityClient_ItemCollectionsNotRegisteredInWorkspace("EdmItemCollection"));
                }
                if (!workspace.IsItemCollectionAlreadyRegistered(DataSpace.SSpace))
                {
                    throw new ArgumentException(Strings.EntityClient_ItemCollectionsNotRegisteredInWorkspace("StoreItemCollection"));
                }
                if (!workspace.IsItemCollectionAlreadyRegistered(DataSpace.CSSpace))
                {
                    throw new ArgumentException(
                        Strings.EntityClient_ItemCollectionsNotRegisteredInWorkspace("StorageMappingItemCollection"));
                }

                // Verify that a factory can be retrieved
                var providerFactory = connection.GetProviderFactory();
                if (providerFactory == null)
                {
                    throw new ProviderIncompatibleException(Strings.EntityClient_DbConnectionHasNoProvider(connection));
                }

                var collection = (StoreItemCollection)workspace.GetItemCollection(DataSpace.SSpace);

                _providerFactory = collection.ProviderFactory;
                Debug.Assert(_providerFactory == providerFactory);
                _initialized = true;
            }

            _metadataWorkspace = workspace;
            _storeConnection = connection;
            _entityConnectionOwnsStoreConnection = entityConnectionOwnsStoreConnection;

            if (_storeConnection != null)
            {
                _entityClientConnectionState = DbInterception.Dispatch.Connection.GetState(_storeConnection, InterceptionContext);
            }

            SubscribeToStoreConnectionStateChangeEvents();
        }
 /// <summary>Retrieves the DbProviderFactory based on the specified DbConnection.</summary>
 /// <returns>The retrieved DbProviderFactory.</returns>
 /// <param name="connection">The connection to use.</param>
 public static DbProviderFactory GetProviderFactory(DbConnection connection)
 {
     Check.NotNull(connection, "connection");
     var factory = connection.GetProviderFactory();
     if (factory == null)
     {
         throw new ProviderIncompatibleException(
             Strings.EntityClient_ReturnedNullOnProviderMethod(
                 "get_ProviderFactory",
                 connection.GetType().ToString()));
     }
     return factory;
 }