diff --git a/MarkMpn.Sql4Cds.Engine/Ado/Sql4CdsCommand.cs b/MarkMpn.Sql4Cds.Engine/Ado/Sql4CdsCommand.cs index d55ca340..38a04df4 100644 --- a/MarkMpn.Sql4Cds.Engine/Ado/Sql4CdsCommand.cs +++ b/MarkMpn.Sql4Cds.Engine/Ado/Sql4CdsCommand.cs @@ -27,6 +27,7 @@ public class Sql4CdsCommand : DbCommand private CommandType _commandType; private CancellationTokenSource _cts; private bool _cancelledManually; + private string _lastDatabase; public Sql4CdsCommand(Sql4CdsConnection connection) : this(connection, string.Empty) { @@ -161,7 +162,7 @@ public override object ExecuteScalar() public override void Prepare() { - if (UseTDSEndpointDirectly || Plan != null) + if (_lastDatabase == _connection.Database && (UseTDSEndpointDirectly || Plan != null)) return; GeneratePlan(true); @@ -199,6 +200,7 @@ public IRootExecutionPlanNode[] GeneratePlan(bool compileForExecution) var plan = _planBuilder.Build(commandText, ((Sql4CdsParameterCollection)Parameters).GetParameterTypes(), out var useTDSEndpointDirectly); UseTDSEndpointDirectly = useTDSEndpointDirectly; + _lastDatabase = _connection.Database; if (compileForExecution) {