Example: CRUD operations using the Amazon SDK for .NET object persistence model - Amazon DynamoDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Example: CRUD operations using the Amazon SDK for .NET object persistence model

The following C# code example declares a Book class with Id, Title, Isbn, and BookAuthors properties. The example uses object persistence attributes to map these properties to the ProductCatalog table in Amazon DynamoDB. The example then uses the DynamoDBContext class to illustrate typical create, read, update, and delete (CRUD) operations. The example creates a sample Book instance and saves it to the ProductCatalog table. It then retrieves the book item and updates its Isbn and BookAuthors properties. Note that the update replaces the existing authors list. Finally, the example deletes the book item.

For more information about the ProductCatalog table used in this example, see Creating tables and loading data for code examples in DynamoDB. For step-by-step instructions to test the following example, see .NET code examples.

Note

The following example doesn't work with .NET core because it doesn't support synchronous methods. For more information, see Amazon asynchronous APIs for .NET.

Example CRUD operations using the DynamoDBContext class
/// <summary> /// Shows how to perform high-level CRUD operations on an Amazon DynamoDB /// table. /// </summary> public class HighLevelItemCrud { public static async Task Main() { var client = new AmazonDynamoDBClient(); DynamoDBContext context = new DynamoDBContext(client); await PerformCRUDOperations(context); } public static async Task PerformCRUDOperations(IDynamoDBContext context) { int bookId = 1001; // Some unique value. Book myBook = new Book { Id = bookId, Title = "object persistence-AWS SDK for.NET SDK-Book 1001", Isbn = "111-1111111001", BookAuthors = new List<string> { "Author 1", "Author 2" }, }; // Save the book to the ProductCatalog table. await context.SaveAsync(myBook); // Retrieve the book from the ProductCatalog table. Book bookRetrieved = await context.LoadAsync<Book>(bookId); // Update some properties. bookRetrieved.Isbn = "222-2222221001"; // Update existing authors list with the following values. bookRetrieved.BookAuthors = new List<string> { " Author 1", "Author x" }; await context.SaveAsync(bookRetrieved); // Retrieve the updated book. This time, add the optional // ConsistentRead parameter using DynamoDBContextConfig object. await context.LoadAsync<Book>(bookId, new DynamoDBContextConfig { ConsistentRead = true, }); // Delete the book. await context.DeleteAsync<Book>(bookId); // Try to retrieve deleted book. It should return null. Book deletedBook = await context.LoadAsync<Book>(bookId, new DynamoDBContextConfig { ConsistentRead = true, }); if (deletedBook == null) { Console.WriteLine("Book is deleted"); } } }
Example
Book class information to add to the ProductCatalog table
/// <summary> /// A class representing book information to be added to the Amazon DynamoDB /// ProductCatalog table. /// </summary> [DynamoDBTable("ProductCatalog")] public class Book { [DynamoDBHashKey] // Partition key public int Id { get; set; } [DynamoDBProperty] public string Title { get; set; } [DynamoDBProperty] public string Isbn { get; set; } [DynamoDBProperty("Authors")] // String Set datatype public List<string> BookAuthors { get; set; } }