数据读取器具有多个字段。多个字段对于存储过程中的EDM原语或枚举类型无效 [英] The data reader has more than one field. Multiple fields are not valid for EDM primitive or enumeration types from a stored procedure

查看:2464
本文介绍了数据读取器具有多个字段。多个字段对于存储过程中的EDM原语或枚举类型无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以看到这已经被问了几次,但是看不到与存储proc调用相关的一个,如下所示,我可以工作。

I can see this has been asked a few times but can't see one where it relates to a stored proc call like below i can work form.

 public ActionResult _NewEmpFifth()
    {

        IEnumerable<SelectListItem> departments = new List<SelectListItem>();
        using (EIPInternalEntities ctx = new EIPInternalEntities())            
        {
            departments = new SelectList(ctx.Database.SqlQuery<string>("EXEC dbo.uspGetDepartments").ToList(), "DepartmentID",  "Department");

            ViewBag.Department = new SelectList(departments, dataValueField:  "DepartmentID", dataTextField:  "Department");        
        }

        var sessionValues = Session["MySessionValues"] as MySessionValues;

        return PartialView();
    }

我知道问题是存储过程返回 DepartmentId 部门这是我想要的。我可以在这个方法下绑定一个文本的值属性吗?

I know the issue is the stored procedure is returning DepartmentId and Department which is what I want. Can I bind those to a text a value property under this method?

我还是新建框架工作和MVC。

I'm still new to frame work and MVC.

推荐答案

想出来。问题是与需要由与存储过程的返回匹配的模型替换。

Figured it out. Issue was with the and needed to be replaced by a model that matched the return from the stored procedure.

新模型

public class GetDepartment
{
    public int DepartmentID { get; set; }
    public string Department { get; set; }

}

更改控制器操作

public ActionResult _NewEmpFifth()
{

    IEnumerable<SelectListItem> departments = new List<SelectListItem>();
    using (EIPInternalEntities ctx = new EIPInternalEntities())           
    {                              

        ViewBag.Department = new SelectList(ctx.Database.SqlQuery<GetDepartment>("EXEC dbo.uspGetDepartments").ToList(), "DepartmentID", "Department");        
    }

    var sessionValues = Session["MySessionValues"] as MySessionValues;

    return PartialView();
}

这篇关于数据读取器具有多个字段。多个字段对于存储过程中的EDM原语或枚举类型无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆