对象持久性模型示例

这个例子由两部分组成:首先,我们必须定义我们的 Book 类型; 第二,我们将它与 DynamoDBContext 一起使用。

[DynamoDBTable("Books")]
class Book
{
    [DynamoDBHashKey]
    public int Id { get; set; }
    public string Title { get; set; }
    public List<string> Authors { get; set; }
    public double Price { get; set; }
}

现在,将它与 DynamoDBContext 一起使用。

var client = new AmazonDynamoDBClient();
DynamoDBContext context = new DynamoDBContext(client);
 
// Store item
Book book = new Book
{
    Title = "Cryptonomicon",
    Id = 42,
    Authors = new List<string> { "Neal Stephenson" },
    Price = 12.95
};
context.Save(book);
 
// Get item
book = context.Load<Book>(42);
Console.WriteLine("Id = {0}", book.Id);
Console.WriteLine("Title = {0}", book.Title);
Console.WriteLine("Authors = {0}", string.Join(", ", book.Authors));