如何在MVC实体框架中为存储过程添加多个参数

本文关键字:添加 存储过程 参数 MVC 实体 框架 | 更新日期: 2024-06-14 09:32:21

我正在将VS2012与MVC4和EntityFramework一起使用。

我需要关于如何以良好的方式向存储过程添加多个参数的建议。我使用下面的代码,但我认为这不是一个很好的编写代码的方法,如果有人知道更好的方法,请分享。

如果我的存储过程参数大于10,那么它看起来不太好。

我的操作方法

    public ActionResult SaveVoucher(Voucher items)
    {
        List<MVC_GetMaxCouponMasterByDept_Result> LastCouponId = db.MVC_GetMaxCouponMasterByDept(
            items.vDeptCode,
            items.vComp,
            items.vFinYear,
            items.vLoc,
            items.CostCenter
            ).ToList<MVC_GetMaxCouponMasterByDept_Result>();
        return View();
    }

如何在MVC实体框架中为存储过程添加多个参数

SP代码应该在一个接受params的助手类中,而不是直接在ActionMethod中,这样它就可以重用了。

PS这还不完整,我不会为你写所有内容,但这应该会对你有所帮助。

就这个问题的性质而言,范围可能会越来越大,即有一百万种方法可以重构更多。。。。所以我建议你多读一些设计模式,因为它们会帮助你完成任务。

public ActionResult SaveVoucher(Voucher items)
{
    SpHelper spHelper = new SpHelper();
    var data  = spHelper.GetMaxCouponMasterByDept(item.vDeptCode);
    return View(data);
}

//this is in its own class file..
public class SpHelper
{
     private DbContext db = new DbContext()
     public List<MVC_GetMaxCouponMasterByDept_Result> GetMaxCouponMasterByDept(string vDeptCode)
     {
         var data = db.MVC_GetMaxCouponMasterByDept(
                    vDeptCode,"CCL", "2015-2016",
                  "Mumbai","10001554"
                  ).ToList();
          return data;
     }
     //... other helpful Sp you have created
}