private bool IsSupportedProvider(Guid source, Guid provider)
        {
            RaiseBeforeIsAdoNetProviderEvent();

            var result = DataConnectionUtils.HasEntityFrameworkProvider(
                _dataProviderManager, provider, _appProject, Services.ServiceProvider) &&
                         DataProviderProjectControl.IsProjectSupported(provider, _appProject);

            RaiseAfterIsAdoNetProviderEvent();

            return(result);
        }
        public void HasEntityFrameworkProvider_returns_false_when_no_legacy_provider_or_ef_reference()
        {
            var provider = new Mock <IVsDataProvider>();

            provider.Setup(p => p.GetProperty("InvariantName")).Returns("System.Data.OleDb");
            var providerGuid = Guid.NewGuid();
            var providers    = new Dictionary <Guid, IVsDataProvider> {
                { providerGuid, provider.Object }
            };
            var dataProviderManager = new Mock <IVsDataProviderManager>();

            dataProviderManager.SetupGet(m => m.Providers).Returns(providers);
            var dte = new MockDTE(".NETFramework,Version=v4.5", references: Enumerable.Empty <Reference>());

            Assert.False(
                DataConnectionUtils.HasEntityFrameworkProvider(
                    dataProviderManager.Object,
                    providerGuid,
                    dte.Project,
                    dte.ServiceProvider));
        }
        public void HasEntityFrameworkProvider_returns_true_when_has_legacy_provider()
        {
            var provider = new Mock <IVsDataProvider>();

            provider.Setup(p => p.GetProperty("InvariantName")).Returns("System.Data.SqlClient");
            var providerGuid = Guid.NewGuid();
            var providers    = new Dictionary <Guid, IVsDataProvider> {
                { providerGuid, provider.Object }
            };
            var dataProviderManager = new Mock <IVsDataProviderManager>();

            dataProviderManager.SetupGet(m => m.Providers).Returns(providers);
            var dte = new MockDTE(".NETFramework,Version=v4.5");

            Assert.True(
                DataConnectionUtils.HasEntityFrameworkProvider(
                    dataProviderManager.Object,
                    providerGuid,
                    dte.Project,
                    dte.ServiceProvider));
        }
Beispiel #4
0
        internal static void PopulateNewFunctionSchemaProcedures(
            Dictionary <EntityStoreSchemaFilterEntry, IDataSchemaProcedure> newFunctionSchemaProcedureMap,
            string designTimeProviderInvariantName,
            string designTimeProviderConnectionString,
            DoWorkEventArgs e               = null,
            BackgroundWorker worker         = null,
            int startingAmountOfProgressBar = 0,
            int amountOfProgressBarGiven    = 100)
        {
            // set up database connection
            var dataConnectionManager = Services.ServiceProvider.GetService(typeof(IVsDataConnectionManager)) as IVsDataConnectionManager;

            Debug.Assert(dataConnectionManager != null, "Could not find IVsDataConnectionManager");

            var dataProviderManager = Services.ServiceProvider.GetService(typeof(IVsDataProviderManager)) as IVsDataProviderManager;

            Debug.Assert(dataProviderManager != null, "Could not find IVsDataProviderManager");

            IVsDataConnection dataConnection = null;

            if (null != dataConnectionManager &&
                null != dataProviderManager)
            {
                dataConnection = DataConnectionUtils.GetDataConnection(
                    dataConnectionManager, dataProviderManager, designTimeProviderInvariantName, designTimeProviderConnectionString);
            }
            if (null == dataConnection)
            {
                throw new ProgressDialogException(
                          string.Format(
                              CultureInfo.CurrentCulture, Design.Resources.RetrievingSprocReturnTypeErrorMessage, "null IVsDataConnection"));
            }

            // open the database connection and collect info for each Function
            try
            {
                dataConnection.Open();
                var dataSchemaServer = new DataSchemaServer(dataConnection);

                // now loop over all entries adding return type information
                var numFunctionFilterEntries      = newFunctionSchemaProcedureMap.Count;
                var numFunctionFilterEntryCurrent = 0;
                foreach (var entry in newFunctionSchemaProcedureMap.Keys.ToList())
                {
                    numFunctionFilterEntryCurrent++;
                    if (worker != null &&
                        e != null &&
                        worker.CancellationPending)
                    {
                        // user requested interrupt of this process
                        e.Cancel = true;
                    }
                    else
                    {
                        if (worker != null &&
                            worker.WorkerReportsProgress)
                        {
                            // report progress so ProgressDialog can update its status
                            var percentCompleted = startingAmountOfProgressBar +
                                                   ((int)
                                                    (((numFunctionFilterEntryCurrent - 1) / (float)numFunctionFilterEntries)
                                                     * amountOfProgressBarGiven));
                            var userState = new ProgressDialogUserState();
                            userState.NumberIterations     = numFunctionFilterEntries;
                            userState.CurrentIteration     = numFunctionFilterEntryCurrent;
                            userState.CurrentStatusMessage = string.Format(
                                CultureInfo.CurrentCulture,
                                Design.Resources.RetrievingSprocReturnTypeInfoMessage,
                                numFunctionFilterEntryCurrent,
                                numFunctionFilterEntries,
                                entry.Schema,
                                entry.Name);
                            worker.ReportProgress(percentCompleted, userState);
                        }

                        // now retrieve and store the return type information
                        var schemaProcedure = dataSchemaServer.GetProcedureOrFunction(entry.Schema, entry.Name);
                        Debug.Assert(
                            null == newFunctionSchemaProcedureMap[entry],
                            "This entry has already been processed, Schema = " + entry.Schema + ", Name = " + entry.Name);
                        newFunctionSchemaProcedureMap[entry] = schemaProcedure;
                    }
                }
            }
            finally
            {
                if (null != dataConnection)
                {
                    dataConnection.Close();
                }
            }
        }
Beispiel #5
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);
                }
            }
        }