예제 #1
0
        // <summary>
        //     DoWork event handler: Get table names in a background thread.
        //     This method is called by background worker component on a different thread than the UI thread.
        //     We use the ModelBuilderEngine to get table names to display
        // </summary>
        private void bgWorkerPopulateTree_DoWork(object sender, DoWorkEventArgs args)
        {
            // This method will run on a thread other than the UI thread.
            // Be sure not to manipulate any Windows Forms controls created on the UI thread from this method.
            using (new VsUtils.HourglassHelper())
            {
                var ssdlAggregator = new DatabaseConnectionSsdlAggregator(Wizard.ModelBuilderSettings);

                var result = new ICollection <EntityStoreSchemaFilterEntry> [3];
                try
                {
                    result[0] = ssdlAggregator.GetTableFilterEntries(args);
                    if (args.Cancel)
                    {
                        return;
                    }

                    result[1] = ssdlAggregator.GetViewFilterEntries(args);
                    if (args.Cancel)
                    {
                        return;
                    }

                    // we don't support function imports in CodeFirst so don't have to retrieve them
                    if (Wizard.ModelBuilderSettings.GenerationOption != ModelGenerationOption.CodeFirstFromDatabase)
                    {
                        result[2] = ssdlAggregator.GetFunctionFilterEntries(args);
                    }

                    if (args.Cancel)
                    {
                        return;
                    }
                }
                finally
                {
                    VsUtils.SafeCloseDbConnectionOnFile(
                        Wizard.ModelBuilderSettings.DesignTimeProviderInvariantName, Wizard.ModelBuilderSettings.DesignTimeConnectionString);
                }

                args.Result = result;
            }
        }
        /// <summary>
        ///     Invoked by the VS Wizard framework when this page is entered.
        ///     Starts a background thread to retrieve table information to display
        /// </summary>
        public override void OnActivated()
        {
            base.OnActivated();

            _ssdlAggregator = new DatabaseConnectionSsdlAggregator(Wizard.ModelBuilderSettings);

            using (new VsUtils.HourglassHelper())
            {
                if (!_modelNamespaceInitialized)
                {
                    var existingNamespaces = InitializeExistingNamespaces(Wizard.Project);

                    // set the storage target to the initial catalog but if we're targeting a database project
                    // then set it to the database name which is by definition unique.
                    var storageTarget = Wizard.ModelBuilderSettings.InitialCatalog;

                    // set the default name for the Model Namespace
                    string trialNamespace;
                    if (String.IsNullOrEmpty(storageTarget))
                    {
                        trialNamespace = ModelConstants.DefaultModelNamespace;
                    }
                    else
                    {
                        trialNamespace = EdmUtils.ConstructValidModelNamespace(
                            storageTarget + ModelConstants.DefaultModelNamespace,
                            ModelConstants.DefaultModelNamespace);
                    }

                    modelNamespaceTextBox.Text = EdmUtils.ConstructUniqueNamespace(trialNamespace, existingNamespaces);
                }
            } // restore cursor

            Debug.Assert(
                Wizard.ModelBuilderSettings.DesignTimeConnectionString != null,
                "Unexpected null value for connection string");

            if (Wizard.ModelBuilderSettings.GenerationOption == ModelGenerationOption.GenerateFromDatabase &&
                (!Wizard.ModelBuilderSettings.DesignTimeConnectionString.Equals(_initializedDataConnection) ||
                 Wizard.ModelBuilderSettings.UseLegacyProvider != _initializedUsingLegacyProvider))
            {
                // Enable the treeView / labels and hide the database project label and textbox.
                databaseObjectTreeView.Visible = true;
                labelPrompt.Visible            = true;

                // Disable wizard navigation temporarily until the background thread completes
                Wizard.EnableButton(ButtonType.Previous, false);
                Wizard.EnableButton(ButtonType.Next, false);
                Wizard.EnableButton(ButtonType.Finish, false);

                // disable model namespace textbox
                modelNamespaceTextBox.Enabled = false;

                // Clear existing database objects
                Wizard.ModelBuilderSettings.DatabaseObjectFilters = null;
                databaseObjectTreeView.TreeViewControl.Nodes.Clear();

                // Put up a status message
                databaseObjectTreeView.ShowStatus(ModelWizard.Properties.Resources.SelectTablesPage_StatusRetrievingTablesText);

                // Get table names in a background thread
                _stopwatch.Reset();
                _stopwatch.Start();
                _bgWorkerPopulateTree.RunWorkerAsync(this);
            }
        }