diff --git a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/ObjectExplorerService.cs b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/ObjectExplorerService.cs index 0a5da3b3..b790a5dc 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/ObjectExplorerService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/ObjectExplorerService.cs @@ -414,6 +414,19 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer } response.Nodes = nodes; response.ErrorMessage = node.ErrorMessage; + try + { + // SMO changes the database when getting sql objects. Make sure the database is changed back to the original one + if (bindingContext.ServerConnection.CurrentDatabase != bindingContext.ServerConnection.DatabaseName) + { + bindingContext.ServerConnection.SqlConnectionObject.ChangeDatabase(bindingContext.ServerConnection.DatabaseName); + } + } + catch(Exception ex) + { + Logger.Write(LogLevel.Warning, $"Failed to change the database in OE connection. error: {ex.Message}"); + // We should just try to change the connection. If it fails, there's not much we can do + } return response; });