如何从 json 结构批量更新 dapper?

How to bulk update in dapper from json struct?

嗨,我的数据在 JSON 结构中是这样的

{
  "Imei": 356980051541947,
  "sendIds": [
    {
      "Id": 13014
    },
    {
      "Id": 13190
    },
    {
      "Id": 13419
    },
    {
      "Id": 13422
    }
  ],
  "ApplicationVersion": 68,
  "GoogleId": "asdsadazxcjkh218",
  "ImagesVersion": "123:1"
}

我的 class 是:

public class PostData
{
    public int ApplicationVersion;
    public long Imei;
    public string GoogleId;
    public string FromDate;
    public string ToDate;
    public string ImagesVersion;
    public ItemId[] SendIds;
}

public class ItemId
{
    public int Id;
}

C# 代码小巧更新:

const string sql = "UPDATE dbo.SentMessage SET IsDelivered=1 WHERE SendID=@Id";
dapperConn.Execute(sql, postData.SendIds);

但是执行时出现如下错误

Must declare the scalar variable \"@Id\

请帮帮我。

发生此异常是因为您 类 中的变量是 Field 而不是 Property。所以你必须将它更改为如下代码的属性:

public class PostData
{
    public int ApplicationVersion { get; set; };
    public long Imei { get; set; };
    public string GoogleId { get; set; };
    public string FromDate { get; set; };
    public string ToDate { get; set; };
    public string ImagesVersion { get; set; };
    public ItemId[] SendIds { get; set; };
}

public class ItemId
{
    public int Id { get; set; };
}