【问题标题】:List of maps in a DynamoDB data modelDynamoDB 数据模型中的映射列表
【发布时间】:2019-11-06 13:53:24
【问题描述】:

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

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

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

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

使用 List&lt;Document&gt; 类型让我最接近,它现在列出了 39 个文档,但所有文档都有 0 个键,0 个值。

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

例如:

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】:

    我知道这是一个很老的东西,但希望这可以帮助其他人在这个问题上挣扎。

    你好德文,

    如果您尝试创建对象以将其发送到您的 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>()
                                            {
                                                { "Address", 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 时,尝试做出贡献感觉很好

    【讨论】:

    • 你是一个救生员。通过 .NET SDK 添加地图的语法令人眼花缭乱,而且他们没有提供任何好的文档来考虑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    • 2016-03-21
    相关资源
    最近更新 更多