Dev*_*von 3 c# datamodel amazon-dynamodb .net-core
我们有一个具有已定义属性的数据模型,但其中一个属性允许动态元数据(地图或字典的列表)。使用文档模型,此属性可以很好地映射到 的列表Document,但是,当我无法将此动态属性映射到使用 的任何内容时DataModel。有没有办法将动态数据映射到模型类属性内的文档?
尝试将其映射为字典列表(与元数据的结构匹配)失败,并出现以下错误:
public List<Dictionary<string, object>> Events { get; set; }
Run Code Online (Sandbox Code Playgroud)
无法将 Amazon.DynamoDBv2.DocumentModel.Document 类型的 [Amazon.DynamoDBv2.DocumentModel.Document] 转换为 System.Collections.Generic.Dictionary`
List<Document>使用一种最接近的类型,它现在列出了 39 个文档,但所有文档都有 0 个键,0 个值。
public List<Document> Events { get; set; }
Run Code Online (Sandbox Code Playgroud)
前任:
document["Events"].AsListOfDocument().First(); // works, contains the keys and values
datamodel.Events.First(); // does not work, it is an empty document
Run Code Online (Sandbox Code Playgroud)
嗨德文郡,
如果您尝试创建对象以将其发送到 DynamoDB,您可以尝试映射任意数据,如.NET 的 AWS 文档中所述
这是文档中的代码:
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);
Run Code Online (Sandbox Code Playgroud)
将数据存入数据库后,您可以将新地图附加到列表中,其中包含以下内容:
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);
}
Run Code Online (Sandbox Code Playgroud)
这对我有用,我希望对其他人也有用。
。。。
PS:顺便说一句,这是我在 Stackoverflow 中的第一个答案,当我多次来到这里寻求节省我时间的答案时,尝试做出贡献感觉很好
| 归档时间: |
|
| 查看次数: |
2727 次 |
| 最近记录: |