// Set the connection string in temporary storage // Returns true if the metadata was successfully loaded for the specified connections internal bool SelectConnectionStringBuilder(EntityConnectionStringBuilderItem selectedConnection, bool resetContainer) { _selectedConnectionStringBuilder = selectedConnection; bool metadataLoaded = false; if (selectedConnection != null) { if (selectedConnection.EntityConnectionStringBuilder != null) { metadataLoaded = _helper.LoadMetadata(selectedConnection.EntityConnectionStringBuilder); } else { // Since we don't have a valid connection string builder, we don't have enough information to load metadata. // Clear any existing metadata so we don't see an old item collection on any subsequent calls that access it. // Don't need to display an error here because that was handled by the caller who created the builder item _helper.ClearMetadata(); } } // Reset the list of containers if requested and set the ComboBox to have no selection. // In some cases the containers do not need to be reset because the caller wants to delay that until a later event or wants to preserve a specific value if (resetContainer) { string defaultContainerName = _selectedConnectionStringBuilder.EntityConnectionStringBuilder == null ? null : _selectedConnectionStringBuilder.EntityConnectionStringBuilder.Name; LoadContainerNames(defaultContainerName, false /*initialLoad*/); } // Update the controls UpdateWizardState(); return(metadataLoaded); }
// Expects that the specified builder item is a named connection already in the list, is a full connection string, or is empty // If empty, the default is to select the named connection option and don't select anything in the list internal void SetConnectionString(EntityConnectionStringBuilderItem connStrBuilderItem) { Debug.Assert(connStrBuilderItem != null, "expected non-null connStrBuilderItem"); _ignoreEvents = true; // set the state of the ConnectionString radio buttons and associated controls bool isNamedConnection = connStrBuilderItem.IsEmpty || connStrBuilderItem.IsNamedConnection; _namedConnectionRadioButton.Checked = isNamedConnection; _namedConnectionComboBox.Enabled = isNamedConnection; _connectionStringRadioButton.Checked = !isNamedConnection; _connectionStringTextBox.Enabled = !isNamedConnection; // set the value of the control that was just enabled if (connStrBuilderItem.IsEmpty) { _namedConnectionComboBox.SelectedIndex = -1; _configureObjectContext.SelectConnectionStringHasValue(false /*connectionStringHasValue*/); } else if (connStrBuilderItem.IsNamedConnection) { _namedConnectionComboBox.SelectedItem = connStrBuilderItem; _configureObjectContext.SelectConnectionStringHasValue(true /*connectionStringHasValue*/); } else { _connectionStringTextBox.Text = connStrBuilderItem.ConnectionString; _configureObjectContext.SelectConnectionStringHasValue(!connStrBuilderItem.IsEmpty); } _ignoreEvents = false; }
// Populates the NamedConnection ComboBox with all of the EntityClient connections from the web.config. // If the specified connectionString is a named connection (if it contains "name=ConnectionName"), it is added to the list and selected. // If the specified connectionString is not a named connection, the plain connection string option is selected and populated with the specified value. private void LoadConnectionStrings() { // Get a list of all named EntityClient connections in the web.config _namedConnections = _helper.GetNamedEntityClientConnections(false /*sortResults*/); EntityConnectionStringBuilderItem connStrBuilderItem = _helper.GetEntityConnectionStringBuilderItem(_entityDataSourceState.ConnectionString); Debug.Assert(connStrBuilderItem != null, "expected GetEntityConnectionStringBuilder to always return non-null"); if (connStrBuilderItem.IsNamedConnection) { // Try to find the specified connection in the list or add it connStrBuilderItem = FindCurrentNamedConnection(connStrBuilderItem); Debug.Assert(connStrBuilderItem != null, "expected a non-null connStrBuilderItem for the named connection because it should have added it if it didn't exist"); } // Sort results now, after we may have added a new item above _namedConnections.Sort(); SelectConnectionStringBuilder(connStrBuilderItem, false /*resetContainer*/); // Update the controls _panel.SetNamedConnections(_namedConnections); _panel.SetConnectionString(_selectedConnectionStringBuilder); }
/// <summary> /// Verify the selected connection string and load the metadata for it if it is successfully verified /// </summary> /// <returns>True if the metadata was successfully loaded from the connection string</returns> private bool VerifyConnectionString() { try { Cursor.Current = Cursors.WaitCursor; if (_connectionInEdit) { bool isNamedConnection = _namedConnectionRadioButton.Checked; Debug.Assert(!isNamedConnection ? _connectionStringRadioButton.Checked : true, "only expecting either named connection or connection string radio button options"); EntityConnectionStringBuilderItem selectedConnection = null; if (isNamedConnection) { if (_namedConnectionComboBox.SelectedIndex != -1) { selectedConnection = _namedConnectionComboBox.SelectedItem as EntityConnectionStringBuilderItem; } } else { // Make a builder item out of the specified connection string. This will do some basic verification on the string. selectedConnection = _configureObjectContext.GetEntityConnectionStringBuilderItem(_connectionStringTextBox.Text); } if (selectedConnection != null) { bool metadataLoaded = _configureObjectContext.SelectConnectionStringBuilder(selectedConnection, true /*resetContainer*/); // If verification failed, try to move the focus back to the appropriate control. if (!metadataLoaded) { if (_namedConnectionRadioButton.Checked) { _namedConnectionComboBox.Select(); } else { if (LocalAppContextSwitches.UseLegacyAccessibilityFeatures || _connectionStringTextBox.TextLength != 0) { _connectionStringTextBox.Select(); _connectionStringTextBox.Select(0, _connectionStringTextBox.TextLength); } } } } // Leave connection edit mode regardless of whether the metadata was loaded or not, because there is no need to keep trying // to validated it over and over again unless the user makes a change that puts it back into edit mode again LeaveConnectionEditMode(); } return(true); } finally { Cursor.Current = Cursors.Default; } }
// Find the current named connection in the list of connections // The returned item may refer to the same connection as the specified item, but it will be the actual reference from the list private EntityConnectionStringBuilderItem FindCurrentNamedConnection(EntityConnectionStringBuilderItem newBuilderItem) { Debug.Assert(_namedConnections != null, "_namedConnections should have already been initialized and should not be null"); Debug.Assert(newBuilderItem != null && newBuilderItem.IsNamedConnection, "expected non-null newBuilderItem"); foreach (EntityConnectionStringBuilderItem namedConnectionItem in _namedConnections) { if (((IComparable <EntityConnectionStringBuilderItem>)newBuilderItem).CompareTo(namedConnectionItem) == 0) { // returning the one that was actually in the list, so we can select it in the control return(namedConnectionItem); } } // didn't find it in the list, so add it _namedConnections.Add(newBuilderItem); return(newBuilderItem); }