本文介绍了实体框架:如何运行存储过程并返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用实体框架执行存储过程,传入变量并返回数据以及返回值?



请不要留下任何代码与实体框架的函数映射。



我已经尝试过这个,我不断得到同样的错误:

以下是我的代码:

  SqlParameter Business = new SqlParameter(Business,Search.Term); 
SqlParameter Location = new SqlParameter(Location,Search.Location);
SqlParameter PageNumber = new SqlParameter(PageNumber,Search.CurrentPage);
SqlParameter RecordsPerPage = new SqlParameter(RecordsPerPage,Search.RecordsPerPage);
SqlParameter TotalRecords = new SqlParameter(TotalRecords,0);

SqlParameter parm = new SqlParameter()
{
ParameterName =@TotalRecords,
Value = 0,
SqlDbType = SqlDbType.Int,
Direction = System.Data.ParameterDirection.Output
};

var List = db.ExecuteStoreQuery< ENT_SearchBusinessResult>(exec usp_BusinessUser_Search,Business,Location,PageNumber,RecordsPerPage,parm);有没有人知道是什么原因造成的?


$ b $ p

$ b

谢谢



编辑:



存储过程代码:

  ALTER PROCEDURE [dbo]。[usp_BusinessUser_Search](@Business nVarChar(255)= NULL 
,@Location nVarChar(255)= NULL
,@PageNumber Int = 1
,@RecordsPerPage Int = 10
,@TotalRecords Int OUTPUT)
AS
BEGIN
SET NOCOUNT ON;

DECLARE @SQL NVARCHAR(MAX)
,@CacheTable SYSNAME
,@TotalRows Int
,@Category VarChar(255)
,@BusinessCategory Int
,@TownCounty VarChar(50)
,@PcodeTownCounty VarChar(50)

INSERT Voucher_SearchHistory(关键字,位置)
SELECT NULLIF(@Business,'') ,NULLIF(@Location,'')


解决方案

帮助:




Is it possible to execute a stored procedure using the Entity Framework, passing in variables and returning data along with a return value?

Please do not leave any code about function mappings with Entity Framework.

I have tried this and I keep getting the same damn error:

Here's my code:

SqlParameter Business = new SqlParameter("Business", Search.Term);
SqlParameter Location = new SqlParameter("Location", Search.Location);
SqlParameter PageNumber = new SqlParameter("PageNumber", Search.CurrentPage);
SqlParameter RecordsPerPage = new SqlParameter("RecordsPerPage", Search.RecordsPerPage);
SqlParameter TotalRecords = new SqlParameter("TotalRecords", 0);

SqlParameter parm = new SqlParameter()
{
    ParameterName = "@TotalRecords",
    Value = 0,
    SqlDbType = SqlDbType.Int,
    Direction = System.Data.ParameterDirection.Output
};

var List = db.ExecuteStoreQuery<ENT_SearchBusinessResult>("exec usp_BusinessUser_Search",Business,Location,PageNumber,RecordsPerPage,parm);

Does anyone have any idea what is causing this?

Thanks

EDIT:

Stored procedure code:

ALTER PROCEDURE [dbo].[usp_BusinessUser_Search] (   @Business nVarChar(255) = NULL
                                                    , @Location nVarChar(255) = NULL
                                                    , @PageNumber Int = 1
                                                    , @RecordsPerPage Int = 10
                                                    , @TotalRecords Int OUTPUT)
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @SQL NVARCHAR(MAX)
            , @CacheTable SYSNAME
            , @TotalRows Int
            , @Category VarChar(255)
            , @BusinessCategory Int
            , @TownCounty VarChar(50)
            , @PcodeTownCounty VarChar(50)

    INSERT  Voucher_SearchHistory (Keyword, Location)
    SELECT  NULLIF(@Business,''), NULLIF(@Location,'')
解决方案

This might help:

http://blogs.msdn.com/b/diego/archive/2012/01/10/how-to-execute-stored-procedures-sqlquery-in-the-dbcontext-api.aspx

这篇关于实体框架:如何运行存储过程并返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 09:37