使用Dapper将存储过程中的字段映射到具有不同名称的类属性 [英] Mapping fields in stored procedure to class properties with different names with Dapper

查看:50
本文介绍了使用Dapper将存储过程中的字段映射到具有不同名称的类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从POCO类的许多领域中发挥了作用:

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

但是我的存储过程向上面的属性返回了不同的名称(在这种情况下,ID之前是

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();

如何指定映射,以表示我希望将存储过程中的"IdStatus"映射到POCO类中的"StatusId",并将"IdCall"映射到"CallId"等?

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?

我无权更改存储过程,因为它们是由DBA控制的,并且较旧的旧系统正在使用它们,如果存储过程中的字段发生了更改,则可能会中断.

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.

赞赏任何想法/想法.

推荐答案

我想到的最接近的事情是将私有属性映射到存储过程返回的列,并使公共属性具有您想要设置的名称和获取那些私有字段:

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

这篇关于使用Dapper将存储过程中的字段映射到具有不同名称的类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆