Dapper-将类名称/属性映射到数据库字段

时间:2019-11-08 04:42:08

标签: c# asp.net dapper

我上课...

public class WireTransferRequest
    {
        public int RequestID { get; set; }
        public DateTime RequestDate { get; set; }
        public string RequesterName { get; set; }
        public string AuthorizerName { get; set; }
        public string AuthorizerSignature { get; set; }
        public DateTime TransferDate { get; set; }
        public string CustomerNames { get; set; }
        public string Reasons { get; set; }
        public string Amounts { get; set; }
        public decimal TotalOfAmounts { get; set; }
    }

我有一张桌子。...

CREATE TABLE IF NOT EXISTS `digital_forms`.`wire_transfer_request` (
  `request_id` INT NOT NULL,
  `request_date` DATETIME NOT NULL,
  `requester_name` VARCHAR(64) NOT NULL,
  `authorizer_name` VARCHAR(64) NULL,
  `authorizer_signature` VARCHAR(64) NULL,
  `transfer_date` DATETIME NULL,
  `customer_names` VARCHAR(64) NULL,
  `customer_reasons` VARCHAR(128) NULL,
  `customer_amounts` VARCHAR(128) NULL,
  `total_of_customer_amounts` DECIMAL(19,2) NULL,
  PRIMARY KEY (`request_id`),
  INDEX `idx_request_date` (`request_date` ASC) VISIBLE,
  INDEX `idx_requestor_name` (`requestor_name` ASC) VISIBLE)
ENGINE = InnoDB

我有一个查询...

    public override void Add(WireTransferRequest request)
        {

            if (request == null)
                throw new ArgumentNullException(nameof(request));

            request.RequestID = _transaction.Connection.ExecuteScalar<int>(

            "INSERT INTO wire_transfer_request(request_date, requester_name, authorizer_name," +
                                             "authorizer_signature, transfer_date, customer_names," +
                                             "customer_reasons, customer_amounts, total_of_customer_amounts" +

            "VALUES (@request_date, @requester_name, @authorizer_name," +
                                             "@authorizer_signature, @transfer_date, @customer_names," +
                                             "@customer_reasons, @customer_amounts, @total_of_customer_amounts; SELECT SCOPE_IDENTITY()",
                                              new
                                             {
                                                 request_date = request.RequestDate,
                                                 requester_name = request.RequesterName,
                                                 authorizer_name = request.AuthorizerName,
                                                 authorizer_signature = request.AuthorizerSignature,
                                                 transfer_date = request.TransferDate,
                                                 customer_names = request.CustomerNames,
                                                 customer_reasons = request.Reasons,
                                                 customer_amounts = request.Amounts,
                                                 total_of_customer_amounts = request.TotalOfAmounts,
                                             },
                                             _transaction
                                             );
        }

我希望Dapper能够正确识别和映射列名和表名。 Google对此进行了快速搜索,发现了很多旧的结果,因此,我很想知道更新的(如果可能的话)解决方案/解决方法。

我尝试使用MatchNamesWithUnderscores映射器,但无法达到我期望的插入效果。

1 个答案:

答案 0 :(得分:0)

我不是Dapper专家,但这是我的想法。

选项0。有效

您的sql中缺少2个)

以下代码段有效。

var sql = "INSERT INTO wire_transfer_request(request_date, requester_name, authorizer_name," +
         "authorizer_signature, transfer_date, customer_names," +
         "customer_reasons, customer_amounts, total_of_customer_amounts)" +

"VALUES (@request_date, @requester_name, @authorizer_name," +
         "@authorizer_signature, @transfer_date, @customer_names," +
         "@customer_reasons, @customer_amounts, @total_of_customer_amounts); SELECT SCOPE_IDENTITY()";

var o = new 
{
    request_date = DateTime.UtcNow,
    requester_name = "X",
    authorizer_name = "Y",
    authorizer_signature = "Sig",
    transfer_date = DateTime.UtcNow,
    customer_names = "Z",
    customer_reasons = "A",
    customer_amounts = "B",
    total_of_customer_amounts = 10m,
};

var inserted = conn.ExecuteScalar(sql, o);

inserted包含新插入的行的request_id

选项1。表的类

Insert(不是此答案的核心)来自官方Dapper.Contrib

[Table("wire_transfer_request")]
public class WireTransferRequest2
{
    public int request_id { get; set; }

    public DateTime request_date { get; set; }

    public string requester_name { get; set; }

    public string authorizer_name { get; set; }

    public string authorizer_signature { get; set; }

    public DateTime transfer_date { get; set; }

    public string customer_names { get; set; }

    public string customer_reasons { get; set; }

    public string customer_amounts { get; set; }

    public decimal total_of_customer_amounts { get; set; }
}
using Dapper.Contrib.Extensions;

(...)

var o = new WireTransferRequest2
{
    request_id = 1,
    request_date = DateTime.UtcNow,
    requester_name = "X",
    authorizer_name = "Y",
    authorizer_signature = "Sig",
    transfer_date = DateTime.UtcNow,
    customer_names = "Z", 
    customer_reasons = "A",
    customer_amounts = "B",
    total_of_customer_amounts = 10m,
};

conn.Insert(o);

选项2。使用ColumnAttribute

Manually map column names with class properties上的详细信息。