When calling Prepare together with EnableOptimizedParameterBinding, TdsExecuteRPC will throw an exception
SQL.ParameterDirectionInvalidForOptimizedBinding because it seems that the Prepare-Method injects addional parameters into the command, that seem to not set the direction correctly.
When debugging the exception i can see it sets the sqlRPC to sp_prepexec and uses 3 additional parameters.
To reproduce
Use the official Prepare-Example from here Link
and add EnableOptimizedParameterBinding = true;
Expected behavior
No exception or a more meaningful one.
I could not find any documentation regarding the issue.
Further technical details
Microsoft.Data.SqlClient 6.1.3 on a net 10 Visual Studio 2026