【问题标题】:DynamoDB C# cannot convert string to integer errorDynamoDB C# 无法将字符串转换为整数错误
【发布时间】:2020-08-29 00:56:12
【问题描述】:

我使用 NET 创建了一个 DynamoDb 并且能够获取项目,这不是一个空列表。我在使用 Postman 的 putitem 上收到状态 400 错误。这是错误:

    "errors": {
        "id": [
            "Could not convert string to integer: 9134d3a0-a6bf-4409-87b3-d9fad02bd31c. Path 'id', line 2, position 44."
        ]
    },

这是我在帖子中使用的正文:

{
    "id":"9134d3a0-a6bf-4409-87b3-d9fad02bd31c",
    "replyDateTime": "63669789320007900",
    "body":"a good body",
    "title":"best title",
    "creator": " James"
}

这是我的可创建代码:

 var request = new CreateTableRequest
            {
                AttributeDefinitions = new List<AttributeDefinition>
                {
                    new AttributeDefinition
                    {
                        AttributeName = "Id",
                        AttributeType = "S"
                    },
                    new AttributeDefinition
                    {
                        AttributeName = "ReplyDateTime",
                        AttributeType = "S"
                    }
                },
                KeySchema = new List<KeySchemaElement>
                {
                    new KeySchemaElement
                    {
                        AttributeName = "Id",
                        KeyType = "HASH" // Partition Key
                    },
                    new KeySchemaElement
                    {
                        AttributeName = "ReplyDateTime",
                        KeyType = "Range" // Sort Key
                    }
                },

这是putitem代码:

public async Task AddNewEntry(string id, string replyDateTime, string body, string title, string creator)
        {
            var queryRequest = RequestBuilder(id, replyDateTime, body, title, creator);

            await PutItemAsync(queryRequest);
        }

        private PutItemRequest RequestBuilder(string id, string replyDateTime, string body, string title, string creator)
        {
            var item = new Dictionary<string, AttributeValue>
            {
                {"Id", new AttributeValue {S = id}},
                {"ReplyDateTime", new AttributeValue {S = replyDateTime}},
                {"Body", new AttributeValue {S = body}},
                 {"Creator", new AttributeValue {S = creator}},
                  {"Title", new AttributeValue {S = title}}
            };

            return new PutItemRequest
            {
                TableName = "BlogDynamoDbTable",
                Item = item
            };
        }

        private async Task PutItemAsync(PutItemRequest request)
        {
            await _dynamoClient.PutItemAsync(request);
        }
    }

我相信我将主键设为字符串。为什么错误消息中甚至提到了整数?

【问题讨论】:

  • {"Id", new AttributeValue {N = id}}说数字不是S,不是N吗?
  • @Marcin,谢谢。我用 S=id 试过了,得到了同样的错误。我更新了问题以表明这一点。

标签: amazon-web-services


【解决方案1】:

我发现了我的错误。模型文件将 id 定义为整数。呸呸呸 我将其更改为字符串并发布。

public class Item
    {
       [Amazon.DynamoDBv2.DataModel.DynamoDBHashKey]
        public string Id { get; set; }

        [Amazon.DynamoDBv2.DataModel.DynamoDBRangeKey]
        public string ReplyDateTime { get; set; }
        public string Body { get; set; }

        public string Title { get; set; }

        public string Creator { get; set; }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    相关资源
    最近更新 更多