[英]Entity framework core Output parameter not working in store procedure using FromSql method (Linq)
I am trying to get output value from the dbcontext
using the method FromSql(...)
. 我正在尝试使用
FromSql(...)
方法从dbcontext
获取输出值。
If i execute it on SSMS, it works but not working in EF core. 如果我在SSMS上执行它,则可以工作,但不能在EF core中工作。
My SP param: 我的SP参数:
@totalItems VARCHAR(MAX) OUTPUT
I've removed other params for readable and point out the issue. 我删除了其他可读性参数并指出了问题。 The records are coming but the
OUTPUT
parameter is always null
. 记录即将到来,但
OUTPUT
参数始终为null
。
Linq LINQ
IQeryable<T> = _context.Set<T>().FromSql("Sp_Todo @totalItems OUTPUT", // i also tried with OUT or without both Out/Output but no luck
new SqlParameter("@totalItems", SqlDbType.Varchar)
{ Direction = ParameterDirection.Output});
I don't' have access to your whole code, but nothing ever gets executed against your queryable source until you try to enumerate it. 我没有访问您的整个代码的权限,但是直到您尝试枚举它之前,都不会对您的可查询源执行任何操作。 So, probably it didn't run the Stored Procedure when you tried to get the OUTPUT.
因此,当您尝试获取输出时,它可能没有运行存储过程。
To force immediate query evaluation you can do it: 要强制立即查询评估,您可以执行以下操作:
IQueryable<T> foo = _context.Set<T>().FromSql("Sp_Todo @totalItems OUTPUT", new SqlParameter("@totalItems", SqlDbType.Varchar) { Direction = ParameterDirection.Output });
//totalItems output still null
var bar = foo.ToList()
//totalItems output not null anymore
Here's how I'm doing it: 这是我的做法:
var _companyCode = new SqlParameter("CompanyCode", "HST");
var _sMsg = new SqlParameter("sMsg", "")
{
Direction = ParameterDirection.Output,
SqlDbType = SqlDbType.VarChar
};
var sql = "exec temp_get_company @CompanyCode, @sMsg OUTPUT";
var result = context.Set<Company>().FromSql(sql, _companyCode, _sMsg).ToList();
var yourOutput = _sMsg.Value.ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.