Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用实体框架调用标量值函数并得到结果_C#_.net_Sql Server_Entity Framework - Fatal编程技术网

C# 如何使用实体框架调用标量值函数并得到结果

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

我正在使用实体框架6.0

我在SQL Server中有标量值函数签名为

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;
    }