Dapper ORM - 插入具有现有 Id 的对象

Dapper ORM - Insert object with existing Id

我正在从一个 API 获取数据,returns 这个对象:

{
  "status": "success",
  "message": "Sucesso",
  "count": 2,
  "items": [
    {
      "id": 18476,
      "date": "2020-12-25",
      "name": "John Doe",
      "phone": "11111111111",
      "text": "Teste de homologação DJN em produção.",
      "process": "00100522320208220501",
      "type": "D"
    }
  ]
}

当我尝试将其插入数据库时​​,我得到的字段“id”等于 NULL。

using (var connection = new SqlConnection(Helper.GetConnectionString("cnn")))
{
    connection.Open();
    var identity = connection.Insert(my_object);
}

使用的“connection.Insert()”方法来自Dapper Contrib Extensions.

我发现您只需要在 Id 字段之前使用此注释:[ExplicitKey]

Reference

using Dapper.Contrib.Extensions;
using System;
using System.Collections.Generic;

//...

[Table("your_table")]
public class Publicacao
{
        [ExplicitKey]
        public long Id { get; set; }
        public DateTime Date { get; set; }
        public string Name{ get; set; }
        public string Phone { get; set; }
        public string Text { get; set; }
        public string Process { get; set; }
        public string Type { get; set; }
}