DynamoDB数据模型中的地图列表

问题描述 投票:1回答:1

我们有一个具有定义属性的数据模型,但是其中一个属性允许动态元数据(地图或词典的列表)。使用文档模型,此属性可以很好地映射到Document的列表,但是,当我无法使此动态属性映射到使用DataModel的任何内容时遇到麻烦。有没有一种方法可以将动态数据映射到模型类属性中的文档?

尝试将其映射为词典列表(与元数据的结构匹配)失败,并出现以下错误:

public List<Dictionary<string, object>> Events { get; set; }

无法转换类型的[Amazon.DynamoDBv2.DocumentModel.Document] Amazon.DynamoDBv2.DocumentModel.Document到 System.Collections.Generic.Dictionary`

使用List<Document>类型最接近我,它现在列出了39个文档,但是所有文档都有0个键,0个值。

public List<Document> Events { get; set; }

Ex:

document["Events"].AsListOfDocument().First(); // works, contains the keys and values

datamodel.Events.First(); // does not work, it is an empty document
c# .net-core amazon-dynamodb datamodel
1个回答
0
投票
嗨德文郡,

如果您试图创建对象以将其发送到DynamoDB,则可以按照AWS documentation for .NET中所述尝试映射任意数据

这里是文档中的代码:

try { DynamoDBContext context = new DynamoDBContext(client); // 1. Create a book. DimensionType myBookDimensions = new DimensionType() { Length = 8M, Height = 11M, Thickness = 0.5M }; Book myBook = new Book { Id = 501, Title = "AWS SDK for .NET Object Persistence Model Handling Arbitrary Data", ISBN = "999-9999999999", BookAuthors = new List<string> { "Author 1", "Author 2" }, Dimensions = myBookDimensions }; context.Save(myBook);

一旦在数据库中保存了数据,您就可以使用以下几行内容将新地图附加到列表中:

var request = new UpdateItemRequest
            {
                TableName = "TableName",
                Key = new Dictionary<string, AttributeValue>() { { "PartitionKey", new AttributeValue { S = "value" } } },
                ExpressionAttributeNames = new Dictionary<string, string>()
                {
                    { "#A", "A" }
                },
                ExpressionAttributeValues = new Dictionary<string, AttributeValue>()
                {
                    {
                        ":val", new AttributeValue
                        {
                            L = new List<AttributeValue>()
                            {
                                {
                                    new AttributeValue
                                    {
                                        M = new Dictionary<string, AttributeValue>()
                                        {
                                            { "Addess", new AttributeValue{S = "Value" } },
                                            { "Latitude", new AttributeValue {  S =  position.Latitude.ToString() } },
                                            { "Longitude", new AttributeValue {  S =  position.Longitude.ToString() } }
                                        }
                                    }
                                }
                            }
                        }
                    }
                },
                UpdateExpression = "SET #A = list_append(#A, :val)"
            };

            try
            {
                var response = await client.UpdateItemAsync(request);

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

这对我有用,我希望对其他人也有用。

。。。

PS:顺便说一句,这是我在Stackoverflow中的第一个答案,当我多次来这里寻找可以节省时间的jajajaja的答案时,尝试做出贡献真是太好了。>

© www.soinside.com 2019 - 2024. All rights reserved.