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

我与影子孤独终老i 提交于 2021-01-28 04:05:08

问题


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.


回答1:


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; }
}
// ...


来源:https://stackoverflow.com/questions/20684310/mapping-fields-in-stored-procedure-to-class-properties-with-different-names-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!