c# - 如何使用 ExecuteSqlInterpolatedAsync() 在 EF Core 3.0 中使用输出参数
问题描述
微软目前没有任何关于输出参数的文档,只有普通的。我正在使用 .Net Core 3.0 和 EF Core 3.0 并尝试从我的存储过程中获取输出参数。代码如下:
var output=""
await _context.Database.ExecuteSqlInterpolatedAsync(
$"EXEC dbo.InsertTest @param1={"test"},@param2={"test2"}, @param3={output});
我不知道的是如何在 3.1 中构造新的 api 以指定输出参数。我想知道是否有人对此有任何文档或在新更新中这样做过。
提前致谢。
解决方案
感谢尤金让我走上正轨。如果有人偶然发现同样的问题:
要在 3.0 中成功获取输出参数,您必须将输出参数专门定义为 SqlParameter(),然后在变量后包含单词“OUT”。
这是使用 ExecuteSqlInterpolated() 的示例
var output = new SqlParameter();
output.ParameterName = "@ID";
output.SqlDbType = SqlDbType.Int;
output.Direction = ParameterDirection.Output;
await _context.Database.ExecuteSqlInterpolated(
"EXEC dbo.InsertTest @param1={'test'},@param2={'test2'}, @param3={output} OUT");
这是使用 ExecuteSqlRawAsync() 的示例
var output = new SqlParameter();
output.ParameterName = "@ID";
output.SqlDbType = SqlDbType.Int;
output.Direction = ParameterDirection.Output;
await _context.Database.ExecuteSqlRawAsync(
"EXEC dbo.InsertTest @param1={0},@param2={1}, @param3={3} OUT", param1,param2,output);
推荐阅读
- jenkins - 在 RHEL8.3 上安装 jenkins
- javascript - 未捕获的 SyntaxError:用于 Shopify 商店的自定义 js 文件中出现意外的令牌 ')'
- amazon-web-services - 获取项目列表的 AWS Appsync Graphql 查询返回空数组,即使 dynamodb 表中有项目
- javascript - Javascript - OpenLayers:合并/合并两行
- excel - 从不同的工作表引用电源查询中的单元格值
- python - 滚动查看多个输入到一个变量
- javascript - Lodash 的 _.add 函数有什么意义?
- javascript - 如果在 React 的数组中找到,则设置 ID
- azure - Microsoft B2C:在发布访问令牌之前运行自定义授权逻辑
- blazor - Blazor:如何有条件地设置元素样式