首页 > 解决方案 > 如何使用 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 以指定输出参数。我想知道是否有人对此有任何文档或在新更新中这样做过。

提前致谢。

标签: c#entity-frameworkasp.net-core

解决方案


感谢尤金让我走上正轨。如果有人偶然发现同样的问题:

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

推荐阅读