我从返回此对象的 API 获取数据:
{
"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 .
最佳答案
我发现你只需要使用这个注解:[ExplicitKey]
之前Id
field 。
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; }
}
关于c# - Dapper ORM - 插入具有现有 ID 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65452831/