How to map the returns of a Store Procedure to the properties of a class?

Asked

Viewed 75 times

0

Viewmodel

  public class ClienteResult
{

    [Display(Name ="intParc_Cod")]
    public int ClienteId { get; set; }

    public string Nome { get; set; }

    public string CPF { get; set; }

    public decimal Saldo { get; set; }

}

Repositorio (Dapper)

    public IEnumerable<ClienteResult> SaldoCliente(int id)
    {
    return
            _context
            .Connection
            .Query<ClienteResult>(
            "EXEC sp_cliente_saldo @Id", new {Id = id } );
    }

Controller

    [HttpGet("v1/cliente/saldocliente/{id:int}")]
    public  IActionResult SaldoCliente(int id)
    {
        return Ok(_repository.SaldoCliente(id));
    }

Good afternoon,

I have a service (Web Api) that has a class (or Viewmodel) that returns the client and their balance. I get this data through an execution of a precedent and then call in a controller that results in JSON.

Well, the problem is that for me to return the JSON with its respective values to my Clienteresult class all its fields have to be the same as the one in the database table. I wanted to return with is in class (e.g., Clienteid, Name) and not as Procedure returns (codigo_cli, cli_name).

I’ve tried Data Annotation [Column("codigo_cli"]] and [Display(Name="Clienteid")], but it didn’t work.

Is there any other alternative?

1 answer

0


The answer is more or less what you tried, but the display is used in the Forms view. What you are looking for is the JsonProperty , example

[JsonProperty(PropertyName = "intParc_Cod")]
public int ClienteId { get; set; }
  • It worked, it was cool. But I ended up using [Datacontract] and [Datamember]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.