Menu
Amazon DynamoDB
Developer Guide (API Version 2012-08-10)

Example: CRUD Operations Using the AWS SDK for .NET Object Persistence Model

The following C# code example declares a Book class with Id, title, ISBN, and Authors properties. It uses the object persistence attributes to map these properties to the ProductCatalog table in DynamoDB. The code example then uses the DynamoDBContext to illustrate typical CRUD operations. The example creates a sample Book instance and saves it to the ProductCatalog table. The example then retrieves the book item, and updates its ISBN and Authors properties. Note that the update replaces the existing authors list. The example finally deletes the book item.

For more information about the ProductCatalog table used in this example, see Creating Tables and Loading Sample Data. For step-by-step instructions to test the following sample, see .NET Code Samples.

using System;
using System.Collections.Generic;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DataModel;
using Amazon.Runtime;

namespace com.amazonaws.codesamples
{
    class HighLevelItemCRUD
    {
        private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();

        static void Main(string[] args)
        {
            try
            {
                DynamoDBContext context = new DynamoDBContext(client);
                TestCRUDOperations(context);
                Console.WriteLine("To continue, press Enter");
                Console.ReadLine();
            }
            catch (AmazonDynamoDBException e) { Console.WriteLine(e.Message); }
            catch (AmazonServiceException e) { Console.WriteLine(e.Message); }
            catch (Exception e) { Console.WriteLine(e.Message); }
        }

        private static void TestCRUDOperations(DynamoDBContext 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.
            context.Save(myBook);
            // Retrieve the book. 
            Book bookRetrieved = context.Load<Book>(bookID);

            // Update few properties.
            bookRetrieved.ISBN = "222-2222221001";
            bookRetrieved.BookAuthors = new List<string> { " Author 1", "Author x" }; // Replace existing authors list with this.
            context.Save(bookRetrieved);

            // Retrieve the updated book. This time add the optional ConsistentRead parameter using DynamoDBContextConfig object.
            Book updatedBook = context.Load<Book>(bookID, new DynamoDBContextConfig { ConsistentRead = true });

            // Delete the book.
            context.Delete<Book>(bookID);
            // Try to retrieve deleted book. It should return null.
            Book deletedBook = context.Load<Book>(bookID, new DynamoDBContextConfig { ConsistentRead = true });
            if (deletedBook == null)
                Console.WriteLine("Book is deleted");

        }
    }

    [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; }
    }
}