【问题标题】:How to bulk update in dapper from json struct?如何从 json 结构批量更新 dapper?
【发布时间】:2016-12-08 05:58:55
【问题描述】:

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

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

我的班级是:

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

但是执行时出现如下错误

必须声明标量变量\"@Id\

请帮帮我。

【问题讨论】:

    标签: c# arrays json dapper bulkupdate


    【解决方案1】:

    发生此异常是因为您的类中的变量是 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; };
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-19
      相关资源
      最近更新 更多