コード例 #1
0
        public void GetProviderManifestTokenConnected_returns_provider_manifest_token()
        {
            var providerServicesMock = new Mock <DbProviderServices>();

            providerServicesMock
            .Protected()
            .Setup <string>("GetDbProviderManifestToken", ItExpr.IsAny <DbConnection>())
            .Returns("FakeProviderManifestToken");

            var mockResolver = new Mock <IDbDependencyResolver>();

            mockResolver.Setup(
                r => r.GetService(
                    It.Is <Type>(t => t == typeof(DbProviderServices)),
                    It.IsAny <string>())).Returns(providerServicesMock.Object);

            Assert.Equal(
                "FakeProviderManifestToken",
                DatabaseGenerationEngine.GetProviderManifestTokenConnected(
                    mockResolver.Object,
                    "System.Data.SqlClient",
                    providerConnectionString: string.Empty));
        }
コード例 #2
0
        internal static void SetupSettingsAndModeForDbPages(
            IServiceProvider sp,
            Project project,
            EFArtifact artifact,
            bool checkDatabaseConnection,
            ModelBuilderWizardForm.WizardMode noConnectionMode,
            ModelBuilderWizardForm.WizardMode existingConnectionMode,
            out ModelBuilderWizardForm.WizardMode startMode,
            out ModelBuilderSettings settings)
        {
            var conceptualEntityModel = artifact.ConceptualModel();

            Debug.Assert(conceptualEntityModel != null, "Null Conceptual Entity Model");
            var entityContainer = conceptualEntityModel.FirstEntityContainer as ConceptualEntityContainer;

            Debug.Assert(entityContainer != null, "Null Conceptual Entity Container");
            var entityContainerName = entityContainer.LocalName.Value;

            // set up ModelBuilderSettings for startMode=noConnectionMode
            startMode = noConnectionMode;
            settings  = new ModelBuilderSettings();
            var appType = VsUtils.GetApplicationType(sp, project);

            settings.VSApplicationType = appType;
            settings.AppConfigConnectionPropertyName = entityContainerName;
            settings.Artifact          = artifact;
            settings.UseLegacyProvider = ModelHelper.GetDesignerPropertyValueFromArtifactAsBool(
                OptionsDesignerInfo.ElementName,
                OptionsDesignerInfo.AttributeUseLegacyProvider,
                OptionsDesignerInfo.UseLegacyProviderDefault,
                artifact);
            settings.TargetSchemaVersion = artifact.SchemaVersion;
            settings.Project             = project;
            settings.ModelPath           = artifact.Uri.LocalPath;

            // Get the provider manifest token from the existing SSDL.
            // We don't want to attempt to get it from provider services since this requires a connection
            // which will severely impact the performance of Model First in disconnected scenarios.
            settings.ProviderManifestToken = DatabaseGenerationEngine.GetProviderManifestTokenDisconnected(artifact);

            // Change startMode and settings appropriately depending on whether there is an existing connection string and whether we can/should connect
            // to the database
            var connectionString = ConnectionManager.GetConnectionStringObject(project, entityContainerName);

            if (connectionString != null)
            {
                var ecsb = connectionString.Builder;
                var runtimeProviderName                = ecsb.Provider;
                var runtimeProviderConnectionString    = ecsb.ProviderConnectionString;
                var designTimeProviderConnectionString = connectionString.GetDesignTimeProviderConnectionString(project);
                var initialCatalog = String.Empty;

                if (checkDatabaseConnection)
                {
                    // This path will check to make sure that we can connect to an existing database before changing the start mode to 'existingConnection'
                    IVsDataConnection dataConnection = null;
                    try
                    {
                        var dataConnectionManager = sp.GetService(typeof(IVsDataConnectionManager)) as IVsDataConnectionManager;
                        Debug.Assert(dataConnectionManager != null, "Could not find IVsDataConnectionManager");

                        var dataProviderManager = sp.GetService(typeof(IVsDataProviderManager)) as IVsDataProviderManager;
                        Debug.Assert(dataProviderManager != null, "Could not find IVsDataProviderManager");

                        if (dataConnectionManager != null &&
                            dataProviderManager != null)
                        {
                            // this will either get an existing connection or attempt to create a new one
                            dataConnection = DataConnectionUtils.GetDataConnection(
                                dataConnectionManager,
                                dataProviderManager,
                                connectionString.DesignTimeProviderInvariantName,
                                designTimeProviderConnectionString);
                            Debug.Assert(
                                dataConnection != null,
                                "Could not find the IVsDataConnection; an exception should have been thrown if this was the case");
                            if (dataConnection != null)
                            {
                                VsUtils.EnsureProvider(runtimeProviderName, settings.UseLegacyProvider, project, sp);

                                if (CanCreateAndOpenConnection(
                                        new StoreSchemaConnectionFactory(),
                                        runtimeProviderName,
                                        connectionString.DesignTimeProviderInvariantName,
                                        designTimeProviderConnectionString))
                                {
                                    startMode      = existingConnectionMode;
                                    initialCatalog = DataConnectionUtils.GetInitialCatalog(dataProviderManager, dataConnection);
                                }
                            }
                        }
                    }
                    catch
                    {
                        // do nothing - we will go to WizardPageDbConfig which is
                        // what we want if the DB connection fails
                    }
                    finally
                    {
                        // Close the IVsDataConnection
                        if (dataConnection != null)
                        {
                            try
                            {
                                dataConnection.Close();
                            }
                            catch
                            {
                            }
                        }
                    }
                }
                else
                {
                    // This path will just parse the existing connection string in order to change the start mode. This is ideal for features
                    // that do not need a database connection -- the information in the connection string is enough.
                    startMode      = existingConnectionMode;
                    initialCatalog = DataConnectionUtils.GetInitialCatalog(
                        connectionString.DesignTimeProviderInvariantName, designTimeProviderConnectionString);
                }

                if (startMode == existingConnectionMode)
                {
                    // the invariant name and connection string came from app.config, so they are "runtime" invariant names and not "design-time"
                    // (Note: it is OK for InitialCatalog to be null at this stage e.g. from a provider who do not support the concept of Initial Catalog)
                    settings.SetInvariantNamesAndConnectionStrings(
                        project,
                        runtimeProviderName,
                        runtimeProviderConnectionString,
                        runtimeProviderConnectionString,
                        false);
                    settings.InitialCatalog = initialCatalog;
                    settings.AppConfigConnectionPropertyName = entityContainerName;
                    settings.SaveConnectionStringInAppConfig = false;

                    VsUtils.EnsureProvider(runtimeProviderName, settings.UseLegacyProvider, project, sp);
                }
            }
        }