从“select”存储过程中检索值的列表/行,其中存储过程函数是从Entity Framework自动生成的

时间:2015-06-29 08:34:04

标签: asp.net-mvc entity-framework sql-server-2008 asp.net-mvc-4 stored-procedures

如何从“select”存储过程函数中检索值的列表/行,其中存储过程函数从Entity Framework自动生成

我无法从存储过程函数中检索值列表,我被困在代码的某些部分,其中存储过程函数是使用实体框架自动生成的 请检查附带的代码,并帮我解决这个问题。 在此先感谢

create procedure [dbo].[SP_MobileList]
(
@MobileId varchar(50) out,
@MobileName varchar(50) out
)
As
Begin
select @MobileId=MobileId,@MobileName=MobileName from BasicMobileData
END

来自实体框架的自动生成代码

//ModelClass
    public partial class BasicMobileData
    {
        public string MobileId { get; set; }
        public string MobileName { get; set; }
        public string MobileIMEno { get; set; }
        public decimal MobilePrice { get; set; }
    }

//In Context
    public virtual DbSet<BasicMobileData> BasicMobileDatas { get; set; }

    //Here it returns single integer value 
    **public virtual int SP_MobileList(ObjectParameter mobileID, ObjectParameter mobileName)
     {
     return((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("SP_MobileList", mobileID, mobileName);
    }**

控制器代码

public class MobileController : Controller
{
    MobileEntities objMobileContext = new MobileEntities();
    BasicMobileData objBasicMobileData = new BasicMobileData();

    #region "ActionMethods"
    public ActionResult MobileDetails()
    {
        ObjectParameter objParMobileId = new ObjectParameter("MobileId",typeof(string));
        ObjectParameter objParMobileName = new ObjectParameter("MobileName", typeof(string));

        //I am Stucked in this part of code
        // this "foreach" will not work since ,autogenerated "SP_MobileList(,)" is returning single integer value"
        foreach(BasicMobileData objBasicMobileData  in objMobileContext.SP_MobileList(objParMobileId,objParMobileName)){}
        return View();
    }
    #endregion        
}

2 个答案:

答案 0 :(得分:0)

我得到了解决方案及其工作

更改了存储过程

create procedure [dbo].[SP_MobileList]
As
Begin
select MobileId,MobileName from BasicMobileData
END

然后自动生成存储过程

 public virtual ObjectResult<SP_MobileIdName_s_Result> SP_MobileIdName_s()
{
            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<SP_MobileIdName_s_Result>("SP_MobileIdName_s");
}

public partial class SP_MobileIdName_s_Result
{
   public string MobileId { get; set; }
   public string MobileName { get; set; }
}

在控制器

public class MobileController : Controller
    {
        MobileEntities objMobileContext = new MobileEntities();
        SP_MobileIdName_s_Result objMobile = new SP_MobileIdName_s_Result();

        #region "ActionMethods"
        public ActionResult MobileDetails()
        {
            foreach (SP_MobileIdName_s_Result objMobile in objMobileContext.SP_MobileIdName_s())
            {
                Response.Write(objMobile.MobileId);
                Response.Write(objMobile.MobileName);
            }
            return View();
        }
        #endregion

    }

答案 1 :(得分:0)

您可以使用:

  

EXEC sp_help SP_MobileList

列出了过程/函数中使用的参数