Mapping fields in stored procedure to class properties with different names with Dapper

dapper sql-server stored-procedures

Question

I have this exert from a POCO class with many fields:

public class Call
{
    public int Id { get; set; }
    public string Customer { get; set; }
    public int StatusId { get; set; }
    public int UserAssignedToId { get; set; }
    public string UserAssignedToName { get; set; }
}

However my stored procedure returns different names to the properties above (in this case the Id is before:

  • IdCall
  • IdStatus
  • IdUserAssignedTo

This is the code I am using to execute the stored procedure:

var call = conn.Query<Call>("CallSPName", new { IdCall = callId }, commandType: CommandType.StoredProcedure).First();

How can I specify a mapping to say I would like "IdStatus" from my stored procedure map to "StatusId" in my POCO class and "IdCall" to "CallId" etc?

I don't have access to change the stored procedures as they are controlled by DBAs and older legacy systems are using them which would break if the fields got changed in the stored procedure.

Any ideas/thoughts appreciated.

Popular Answer

The closest thing which comes to my mind is to have private properties mapped to columns returned by the stored procedure and make the public properties with the names you want setting and getting those private fields:

// ...
private int IdStatus;
public int StatusId {
    get { return IdStatus; }
    set { IdStatus = value; }
}
// ...


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why