ObjectContext.ExecuteStoreCommand how to clear parameters between calls?

I make several calls to ObjectContext.ExecuteStoreCommand with different commands and different parameters, although I use the same parameter list (object) for several commands. I get the following exception:

System.ArgumentException: The SqlParameter is already contained by another SqlParameterCollection.

Is there a way to clear the parameters between calls, as I would do with ADO.NET?

Updated with sample code:

    string sqlDeleteWebUserGreen = "delete WebUserGreen where WebUserId = @WebUserId";
    string sqlDeleteWebUserBlue = "delete WebUserBlue where WebUserId = @WebUserId";

    var argsDeleteWebUserXref = new DbParameter[] {
        new SqlParameter { ParameterName = "WebUserId", Value = user.WebUserId }

    rowsAffectedDeleteWebUserXref += base.context.ExecuteStoreCommand(sqlDeleteWebUserGreen, argsDeleteWebUserXref);
    rowsAffectedDeleteWebUserXref += base.context.ExecuteStoreCommand(sqlDeleteWebUserBlue, argsDeleteWebUserXref);

UPDATE

Basically, I cannot find a better way to do this, so in the end I accepted the answer below. The only difference is that I just put the creation of the parameter in a separate method, so my calls look likebase.context.ExecuteStoreCommand(sqlDeleteWebUserBlue, MethodThatWillGiveMeTheParameterArray());

+3
source share
1 answer

The problem is that you are using the same parameter twice.

var argsDeleteWebUserXref1 = new DbParameter[] {         new SqlParameter { ParameterName = "WebUserId", Value = user.WebUserId }  

var argsDeleteWebUserXref2 = new DbParameter[] {         new SqlParameter { ParameterName = "WebUserId", Value = user.WebUserId }  

rowsAffectedDeleteWebUserXref += base.context.ExecuteStoreCommand(sqlDeleteWebUserGreen, argsDeleteWebUserXref1);      
rowsAffectedDeleteWebUserXref += base.context.ExecuteStoreCommand(sqlDeleteWebUserBlue, argsDeleteWebUserXref2);
+2

Source: https://habr.com/ru/post/1791505/


All Articles