【问题标题】:How to retrieve result from a stored procedure using entity-framework in MVC如何使用 MVC 中的实体框架从存储过程中检索结果
【发布时间】:2018-02-21 16:40:24
【问题描述】:

我有一个存储过程sp_ValidateUser,它返回两列UserIDRoleID

我创建的函数导入使用了以下设置:

MVC 代码:

[HttpPost]
[AllowAnonymous]
public ActionResult Index(tblUser user)
{
    SchoolDBEntities usersEntities = new SchoolDBEntities();
    int? userId = usersEntities.fnValidateUser(user.Username, user.Password).FirstOrDefault(); //Check if correct username and password

    string message = string.Empty;

    if (userId.RoleID == 1) //returns error
    {
        //Redirect to URL
    }

    return View(user);
}

如何从sp_ValidateUser_Result 的返回集合中同时检索RoleIDUserID

【问题讨论】:

标签: asp.net-mvc entity-framework


【解决方案1】:

由于您已经从使用 Function Import 生成的存储过程的方法调用中返回了一个名为 sp_ValidatUser_Result 的复杂类型,因此您应该能够得到如下结果:

sp_ValidatUser_Result result = usersEntities.fnValidateUser(user.Username, user.Password).FirstOrDefault(); //Check if correct username and password

string message = string.Empty;

if (result != null && result.RoleID == 1)
{
     //Redirect to URL
}

【讨论】:

    【解决方案2】:

    我已经构建了一个存储过程,它以 id 数组作为输入参数并返回数据表(在我的例子中,只有自引用表中子元素的 id)Take a look at the code here it's using EF and code first approach

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多