首页 > 代码库 > [转]Entity FrameWork利用Database.SqlQuery<T>执行存储过程并返回参数

[转]Entity FrameWork利用Database.SqlQuery<T>执行存储过程并返回参数

本文转自;http://www.cnblogs.com/xchit/p/3334782.html

 

目前,EF对存储过程的支持并不完善。存在以下问题:
        EF不支持存储过程返回多表联合查询的结果集。
        EF仅支持返回返回某个表的全部字段,以便转换成对应的实体。无法支持返回部分字段的情况。
        虽然可以正常导入返回标量值的存储过程,但是却没有为我们自动生成相应的实体.cs代码,我们还是无法在代码中直接调用或使用标量存储过程
        EF不能直接支持存储过程中Output类型的参数。
        其他一些问题。
今天我们利用EF执行sql语句的方式来执行存储过程,并得到OutPut的值。

  首先新建存储过程:

    

?
Create PROCEDURE proc_testEF
  (
    @id int,
    @voteCount int OUTPUT --返回值
  )
  AS
  BEGIN
    SELECT @voteCount = COUNT(*)
    FROM ConfirmItem
    WHERE ConfirmItemID = @id;
    select * from ConfirmItem where ConfirmItemID=@id;
  END
  

  然后写EF调用方法:

  

?
using (DBEntities context = new DBEntities())
           {
               var idParam = new System.Data.SqlClient.SqlParameter
               {
                   ParameterName = "@id",
                   Value = http://www.mamicode.com/1
               };
               var votesParam = new System.Data.SqlClient.SqlParameter
               {
                   ParameterName = "@voteCount",
                   Value = http://www.mamicode.com/0,
                   Direction = ParameterDirection.Output
               };
               var results = context.Database.SqlQuery<Models.ConfirmItem>(
                   "proc_testEF @id, @voteCount out",
                    idParam,
                   votesParam);
 
 
               var person = results.Single();
               var votes = (int)votesParam.Value; //得到OutPut类型值
               return votes;
           }

  测试多次,没有什么问题;最后我对方法进行封装:

  

?
/// <summary> 
/// 执行原始SQL命令 
/// </summary> 
/// <param name="commandText">SQL命令</param> 
/// <param name="parameters">参数</param> 
/// <returns>影响的记录数</returns> 
public Object[] ExecuteSqlNonQuery<T>(string commandText, params Object[] parameters)
{
    using (DBEntities context = new DBEntities())
    {
        var results = context.Database.SqlQuery<T>(commandText, parameters);
        results.Single();
        return parameters;
    }
}

  调用方式:

  

?
var idParam = new System.Data.SqlClient.SqlParameter
               {
                   ParameterName = "@id",
                   Value = http://www.mamicode.com/1
               };
               var votesParam = new System.Data.SqlClient.SqlParameter
               {
                   ParameterName = "@voteCount",
                   Value = http://www.mamicode.com/0,
                   Direction = ParameterDirection.Output
               };
               System.Data.SqlClient.SqlParameter[] parm = {
                                                           idParam,
                                                           votesParam
                                                           };
               parm = (System.Data.SqlClient.SqlParameter[])new BLL.Usual.ConfirmItemManager().ExecuteSqlNonQuery<Models.ConfirmItem>("proc_testEF @id, @voteCount out", parm);
               
               string s = parm[1].Value.ToString();

  当然还有其它的方式,只是觉得这种简单方便,比起添加实体模型来说,要方便很多!

[转]Entity FrameWork利用Database.SqlQuery<T>执行存储过程并返回参数