如何从Web API控制器调用存储过程?
我有以下代码使用Web API从SQL Server数据库中获取数据,我的api可以正常工作,但我想改用存储过程。
存储过程名称为uspStaffDirectoryGetallXXXEmployeeInfo
,我将其导入为.edmx
到GetEmployeeInformation
中。
注意:存储过程包含来自另一个数据库的字段。
这是我正在使用的代码,但我想使用存储过程替换它。
private EmployeesEntities db = new EmployeesEntities();
public IHttpactionResult GetEmployees()
{
var query = (from emp in db.employees
select new
{
emp.employee_number,emp.employee_photo,emp.first_name,emp.last_name,});
var employees = query.ToList();
return Ok(employees);
}
这是我用来使用存储过程uspStaffDirectoryGetallXXXXEmployeeInfo
获取数据的代码:
private EmployeesEntities db = new EmployeesEntities();
//[HttpGet]
public List<uspStaffDirectoryGetallXXXXEmployeeInfo_Result> Get()
{
List<uspStaffDirectoryGetallXXXXEmployeeInfo_Result> emplist = new List<uspStaffDirectoryGetallXXXXEmployeeInfo_Result>();
using (EmployeesEntities db = new EmployeesEntities())
{
var results = db.uspStaffDirectoryGetallXXXXEmployeeInfo();
foreach (var result in results)
{
var employee = new uspStaffDirectoryGetallXXXXEmployeeInfo_Result()
{
id = result.id,Employee_Display_Name = result.Employee_Display_Name,first_name = result.first_name,last_name = result.last_name,Employee_Phone = result.Employee_Phone,Employee_Email = result.Employee_Email
};
emplist.Add(employee);
}
return emplist;
}
但是,当我运行API时,我不断收到此错误,我在任何地方都没有名为“ Column1”的字段
System.Data.Entity.Core.EntityCommandExecutionException:'数据读取器与指定的'EmployeesModel.uspStaffDirectoryGetallXXXXEmployeeInfo_Result不兼容。类型为“ Column1”的成员在数据读取器中没有具有相同名称的对应列。”
在这条线上
public virtual ObjectResult<uspStaffDirectoryGetallXXXEmployeeInfo_Result> uspStaffDirectoryGetallXXXXEmployeeInfo()
{
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<uspStaffDirectoryGetallXXXXEmployeeInfo_Result>("uspStaffDirectoryGetallXXXXEmployeeInfo");
}
我错过了什么?我该如何解决?