Ad

Search This Blog

Friday, June 5, 2015

How to get the data using Stored Procedure using Entity frame work with Code First Approach in MVC



By Using Below Code we can get the data from Data base.

 public List<DTO_Approval> GetSApprovalData_Status_New()
        {
            var SApproval = new List<DTO_Approval>();
            using (var db = new SContext())
            {
                using (db.Database.Connection)
                {
                     var context = ((IObjectContextAdapter)db).ObjectContext;
                    db.Database.Connection.Open();
                    DbCommand cmd = db.Database.Connection.CreateCommand();
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.CommandText = "USP_GetSupplierApprovalData";
                    SApproval = context.Translate<DTO_SApproval>(cmd.ExecuteReader()).ToList();
                    db.Database.Connection.Close();
                }
            }
            return SApproval;
           
        }

Calling From Controller :-


 public ActionResult NewLoggedSuppliers()
        {
            List<DTO_SApproval> sApproval = new List<DTO_SApproval>();
            var sRepository = new SRepository();
            sApproval = sRepository.GetSApprovalData_Status_New();

            if (sApproval.Count > 0)
            {

                return View(sApproval.ToList());
            }
            else
            {
                return Redirect(Url.Action("", "Suppliers") + "/Create");
            }

        }

No comments:

Post a Comment