C# 如何使用实体框架调用标量值函数并得到结果
我正在使用实体框架6.0 我在SQL Server中有标量值函数签名为C# 如何使用实体框架调用标量值函数并得到结果,c#,.net,sql-server,entity-framework,C#,.net,Sql Server,Entity Framework,我正在使用实体框架6.0 我在SQL Server中有标量值函数签名为 fn_CartAmount(@CartId bigint) RETURNS decimal(18,2) 我想从实体框架调用此函数,并获取其结果。您可以尝试以下方法: public decimal GetCartAmount(int cartId) { var returnCode = new SqlParameter("@ReturnCode", SqlDbType.Bit) { Dire
fn_CartAmount(@CartId bigint) RETURNS decimal(18,2)
我想从实体框架调用此函数,并获取其结果。您可以尝试以下方法:
public decimal GetCartAmount(int cartId)
{
var returnCode = new SqlParameter("@ReturnCode", SqlDbType.Bit) { Direction = ParameterDirection.Output };
object[] parameters =
{
new SqlParameter(@"CartId", SqlDbType.BigInt) {Value = cartId}
, returnCode
};
string command = string.Format("exec @ReturnCode = dbo.fn_CartAmount @CartId");
Database.ExecuteSqlCommand(command, parameters);
return (decimal)returnCode.Value;
}