ObjectContext.ExecuteStoreCommand,如何清晰的通话之间的参数? [英] ObjectContext.ExecuteStoreCommand, how to clear parameters between calls?

查看:151
本文介绍了ObjectContext.ExecuteStoreCommand,如何清晰的通话之间的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在多个调用用不同的命令和不同的参数ObjectContext.ExecuteStoreCommand,虽然我使用了若干命令的相同的参数列表(对象)。我收到以下异常:

I am making multiple calls to ObjectContext.ExecuteStoreCommand with different commands and different parameters, although I use the same parameter list (object) for a several of the commands. I am getting the following exception:

System.ArgumentException:SqlParameter有已被另一SqlParameterCollection包含

有没有办法来调用之间明确的参数,因为我将与直线上升ADO.NET办?

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

更新以code样品:<​​/ STRONG>

Updated with a code sample:

    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);

更新

基本上,我无法找到这样做的没有更好的办法,所以我最终接受下面的答案。唯一的区别是,我只是把参数的创建到一个单独的方法,所以我的电话看起来像 base.context.ExecuteStoreCommand(sqlDeleteWebUserBlue,MethodThatWillGiveMeTheParameterArray());

Basically I am unable to find any better way of doing this, so I ended up accepting the answer below. The only difference is that I simply put the creation of the parameter into a separate method, so my calls look like base.context.ExecuteStoreCommand(sqlDeleteWebUserBlue, MethodThatWillGiveMeTheParameterArray());

推荐答案

的问题是,您使用的是相同的参数两次。

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);

这篇关于ObjectContext.ExecuteStoreCommand,如何清晰的通话之间的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆