简体   繁体   English

Dapper 动态参数

[英]Dapper dynamic parameter

I am using dapper and dynamicParameters to get data from SQL Server in a Web API project (.NET 5).我正在使用 dapper 和 dynamicParameters 从 Web API 项目(.NET 5)中的 SQL 服务器获取数据。

It works most of the time without any problems, but sometimes I get the following error它大部分时间都可以正常工作,但有时我会收到以下错误

Procedure or function MySP has too many arguments specified.程序或 function MySP 指定了太多 arguments。

I suspect due to simultaneous requests, the parameters are duplicated.我怀疑由于同时请求,参数是重复的。 Is there a solution to this problem?这个问题有解决方案吗?

string sp = "MySP";

if (ADOFunctions.IsConncetionReadyToOpen(Connection))
{
    await Connection.OpenAsync();
}

var queryParameters = new DynamicParameters();
queryParameters.Add("@Date", dateTime.Date);
queryParameters.Add("@ClientRef", clientRef);
queryParameters.Add("@orderRefs", orderRefList);

var countLimitation = await Connection.QueryAsync<UnityExhibitionOrdersInfo>
   (sql: sp, param: queryParameters, commandType: System.Data.CommandType.StoredProcedure);

foreach (var item in countLimitation)
{
    if (item.Sold > item.Limit)
    {
        returnVal.Message += $"error ";
        returnVal.Result = false;
    }
}

Firstly, it should be noted that you don't even need DynamicParameters here;首先,需要注意的是,这里甚至不需要DynamicParameters a simple:一个简单的:

new { dateTime.Date, ClientRef = clientRef, orderRefs = orderRefList }

should be more than sufficient for the parameters, since this is a simple usage, but: either way, your queryParameters object is local and not shared (which is good).对于参数来说应该绰绰有余,因为这是一个简单的用法,但是:无论哪种方式,您的queryParameters object 都是本地的而不是共享的(这很好)。 This should work absolutely fine - my suspicion, then, would be: "is Connection somehow shared between requests / concurrent async flows on the same request?"应该工作得很好——然后,我的怀疑是:“ Connection是否以某种方式在同一请求上的请求/并发异步流之间共享?” (it absolutely must not be; ADO.NET connections are not thread-safe and are not intended for concurrent access). (绝对不能这样;ADO.NET 连接不是线程安全的,不适合并发访问)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM