如何使用EF将参数传递给mvc 3中的存储过程?

时间:2012-04-20 06:52:21

标签: asp.net-mvc asp.net-mvc-3 entity-framework stored-procedures

我有mvc 3应用程序,其中在会话对象中我正在获取所需的所有值 以我作为执行存储过程的参数。

如果userAction更新,则执行存储过程。

  public ActionResult Index(string userAction)
    {
       if(Session["Mappings"] != null)           
            ViewData["Message"] = "Mapping web grid";

        if (Session["PricingSecurities"] == null)
            Session["PricingSecurities"] = objRepository.GetPricingSecurityID();
        if (Session["Cusips"] == null)
            Session["Cusips"] = objRepository.GetCUSIP();

        SecurityMappingModel objModel = null;
        mappings = (List<SecurityMappingModel>)Session["Mappings"];

        objModel = new SecurityMappingModel();


        if (userAction == "Update" )
        {
            //please tell me how can i take values from Session[Mappings] and pass it to stored procedure?

            return RedirectToAction("Index");
        }
        objModel.PricingSecirities = (List<SelectListItem>)Session["PricingSecurities"];
        objModel.Cusips = (List<SelectListItem>)Session["Cusips"];
        ViewBag.Mappings = mappings;
        return View(objModel);
    }

如何从Session [Mappings]获取值并将其传递给存储过程?

1 个答案:

答案 0 :(得分:2)

你需要做这样的事情:

    using (MyEntitiesDataModel context = new MyEntitiesDataModel())
    {
        context.ExecuteStoreCommand(
            "exec MyStoredProc @param1={0}, @param2={1}",
            Session["foo"], Session["bar"]);
    }

... MyEntitiesDataModel 是您的EF数据模型, MyStoredProc 是您存储过程的名称。