我在Repository类中编写了一个方法,我在Web API项目中为其创建了一个Controller。 以下是我的Model类:
namespace Meijer.Merch.Space.Data
{
public class FloorplanAnalyst
{
public int EmpId { get; set; }
public string EmpName { get; set; }
}
}
以下是Repository类中的方法:
public IEnumerable<FloorplanAnalyst> GetFloorplanAnalyst()
{
return this.Database.SqlQuery<FloorplanAnalyst>("GET_FLOORPLAN_ANALYSTS").ToList();
}
以下是我的控制器方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace SpaceWebApi.Controllers
{
/// <summary>
/// Controller used for the retrieving of FloorplanAnalyst information
/// </summary>
public class FloorplanAnalystController : ApiController
{
private ISpaceRepository db = new SpaceRepository();
/// <summary>
/// Returns a list of FloorplanAnalysts.
public IEnumerable<FloorplanAnalyst> GetFloorplanAnalyst()
{
return db.GetFloorplanAnalyst();
}
}
}
但是我得到了跟踪记录作为我的结果(返回27行):
{
"EmpId": 0,
"EmpName": null
},
{
"EmpId": 0,
"EmpName": null
}
}
在SQL Server中,执行存储过程时会返回正确的数据。但是在Web API端,它是不正确的。
答案 0 :(得分:1)
您遇到此问题,因为EmpId
类中的EmpName
和FloorplanAnalyst
属性与GET_FLOORPLAN_ANALYSTS
存储过程返回的列名称不匹配。
要解决此问题,请确保每个属性在存储过程的结果查询中都具有关联的列名(与属性相同的名称)。