用于存储过程的ASP.NET Web Api控制器不返回数据

时间:2016-08-03 22:35:12

标签: c# asp.net-mvc asp.net-web-api

我的数据库中有以下存储过程:

SELECT dbo.Persona.Nombre, dbo.Persona.Cedula, dbo.Prestamo.Empresa, dbo.Prestamo.Concepto, dbo.Prestamo.Monto, dbo.Prestamo.Fecha, dbo.Prestamo.PagadoATiempo
FROM dbo.Financiera INNER JOIN
     dbo.Prestamo ON dbo.Financiera.RNC = dbo.Prestamo.Empresa INNER JOIN
     dbo.Persona ON dbo.Prestamo.Cliente = dbo.Persona.Cedula
WHERE dbo.Persona.Cedula = @p1 

然后在我的上下文类中:

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

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

    public virtual DbSet<Financiera> Financieras { get; set; }
    public virtual DbSet<Persona> Personas { get; set; }
    public virtual DbSet<Prestamo> Prestamoes { get; set; }

    public virtual ObjectResult<EnviarHistorial_Result> EnviarHistorial(string p1)
    {
        var p1Parameter = p1 != null ?
            new ObjectParameter("p1", p1) :
            new ObjectParameter("p1", typeof(string));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<EnviarHistorial_Result>("EnviarHistorial", p1Parameter);
    }

    public virtual ObjectResult<Nullable<bool>> EnviarSalud(string p1)
    {
        var p1Parameter = p1 != null ?
            new ObjectParameter("p1", p1) :
            new ObjectParameter("p1", typeof(string));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Nullable<bool>>("EnviarSalud", p1Parameter);
    }
}

我正在使用以下控制器访问它:

namespace Web_Services.Controllers
{
    public class HistorialController : ApiController
    {
        private BDWSEntities db = new BDWSEntities();
        public IHttpActionResult GetHistorial(string Cedula)
        {

            var record = db.EnviarHistorial(Cedula);
            if (record == null)
            {
                return NotFound();
            }
            return Ok(record);
        }
    }
}

现在,理论上,这个控制器应该返回数据,但是当通过相关的localhost / api / Historial访问时,它不会返回任何数据或消息,它只返回一个空的JSON文件。发生了什么?

1 个答案:

答案 0 :(得分:0)

试试这个:

    public virtual ObjectResult<EnviarHistorial_Result> EnviarHistorial(string p1)
    {
        var p1Parameter = p1 != null ?
            new ObjectParameter("p1", p1) :
            new ObjectParameter("p1", typeof(string));

        return this.Database.SqlQuery<EnviarHistorial_Result>("EnviarHistorial", p1);
    }