Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MVC中调用存储过程_C#_Sql Server_Asp.net Mvc_Asp.net Mvc 4_Stored Procedures - Fatal编程技术网

C# 在MVC中调用存储过程

C# 在MVC中调用存储过程,c#,sql-server,asp.net-mvc,asp.net-mvc-4,stored-procedures,C#,Sql Server,Asp.net Mvc,Asp.net Mvc 4,Stored Procedures,我有一个存储过程,我正试图调用它来将结果返回到视图中。我的models文件夹中有一个类,我正试图从中调用它 public class MasterMenu { public List<USP_MenuList_ForUser_G_Result> GetMenus(int userId) { List<USP_MenuList_ForUser_G_Result> GetMenuListForUser

我有一个存储过程,我正试图调用它来将结果返回到视图中。我的models文件夹中有一个类,我正试图从中调用它

 public  class MasterMenu
    {

        public  List<USP_MenuList_ForUser_G_Result> GetMenus(int userId)
        {


            List<USP_MenuList_ForUser_G_Result> GetMenuListForUser = null;
            using (MenuEntities dataContext = new MenuEntities())
            {
                GetMenuListForUser = dataContext.USP_MenuList_ForUser_G(56367).ToList();
                return GetMenuListForUser;
            }
        }
我还有一个类,它看起来像一个db上下文类,但在我创建EDMX时自动生成

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Adds.Areas.Mvc.Menu.Models
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Entity.Core.Objects;
    using System.Linq;

    public partial class MenuEntities : DbContext
    {
        public MenuEntities()
            : base("name=MenuEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<tblAgent> tblAgents { get; set; }
        public virtual DbSet<tblMenu> tblMenus { get; set; }
        public virtual DbSet<tblQualifier> tblQualifiers { get; set; }
        public virtual DbSet<tblRoleMenuMapping> tblRoleMenuMappings { get; set; }
        public virtual DbSet<tblRole> tblRoles { get; set; }
        public virtual DbSet<tblUserRoleMapping> tblUserRoleMappings { get; set; }
        public virtual DbSet<tblUsersLogin> tblUsersLogins { get; set; }
        public virtual DbSet<tblUserMenuMapping> tblUserMenuMappings { get; set; }

        public virtual ObjectResult<USP_MenuList_ForUser_G_Result> USP_MenuList_ForUser_G(Nullable<int> userID)
        {
            var userIDParameter = userID.HasValue ?
                new ObjectParameter("UserID", userID) :
                new ObjectParameter("UserID", typeof(int));

            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<USP_MenuList_ForUser_G_Result>("USP_MenuList_ForUser_G", userIDParameter);
        }
    }
}
//------------------------------------------------------------------------------
//


您的控制器必须使其对视图可用,例如传递到强类型视图或ViewBag中的部分模型。因为它看起来是在全局布局或局部视图中进行的,或者是因为它是一个始终可见的菜单,您可以考虑将代码包添加到ViewBag中的全局筛选器中,该全局筛选器添加到您的Global中的站点。AXA.< /P>看来,根据生成的上下文,他已经导入了该函数。谢谢你的关注
public class MasterMenuController : Controller
    {
        private MenuEntities menuEntities = new MenuEntities();
}
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Adds.Areas.Mvc.Menu.Models
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Entity.Core.Objects;
    using System.Linq;

    public partial class MenuEntities : DbContext
    {
        public MenuEntities()
            : base("name=MenuEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<tblAgent> tblAgents { get; set; }
        public virtual DbSet<tblMenu> tblMenus { get; set; }
        public virtual DbSet<tblQualifier> tblQualifiers { get; set; }
        public virtual DbSet<tblRoleMenuMapping> tblRoleMenuMappings { get; set; }
        public virtual DbSet<tblRole> tblRoles { get; set; }
        public virtual DbSet<tblUserRoleMapping> tblUserRoleMappings { get; set; }
        public virtual DbSet<tblUsersLogin> tblUsersLogins { get; set; }
        public virtual DbSet<tblUserMenuMapping> tblUserMenuMappings { get; set; }

        public virtual ObjectResult<USP_MenuList_ForUser_G_Result> USP_MenuList_ForUser_G(Nullable<int> userID)
        {
            var userIDParameter = userID.HasValue ?
                new ObjectParameter("UserID", userID) :
                new ObjectParameter("UserID", typeof(int));

            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<USP_MenuList_ForUser_G_Result>("USP_MenuList_ForUser_G", userIDParameter);
        }
    }
}