User guide

int id = item["Id"].AsInt();
Or, you could simply perform an explicit cast here by using (int):
int id = (int)item["Id"];
For more information about data type conversions with DynamoDB, see DynamoDB Data Types and
DynamoDBEntry.
For more information and examples about the DynamoDB document model, see .NET: Document Model.
Object Persistence
The object persistence programming model is specifically designed for storing, loading, and querying
.NET objects in DynamoDB.You access this model through the Amazon.DynamoDBv2.DataModel
namespace.
Of the three models, the object persistence model is easiest to code against whenever you are storing,
loading, or querying DynamoDB data. For example, you work with DynamoDB data types directly. However,
this model provides access only to operations that store, load, and query .NET objects in DynamoDB.
For example, you can use this model to create, retrieve, update and delete items in tables. However, you
must first create your tables using the low-level model, and then you can use this model to map your
.NET classes to the tables.
The following example shows how to define a .NET class that represents an item in a table in DynamoDB:
// using Amazon.DynamoDBv2.DataModel;
[DynamoDBTable("AnimalsInventory")]
class Item
{
[DynamoDBHashKey]
public int Id { get; set; }
[DynamoDBRangeKey]
public string Type { get; set; }
public string Name { get; set; }
}
In the preceding example, the DynamoDBTable attribute specifies the table name, while the
DynamoDBHashKey and DynamoDBRangeKey attributes model the table's hash-and-range primary key.
The following example shows how to use an instance of this .NET class to insert an item into a table in
DynamoDB:
// using Amazon.DynamoDBv2;
// using Amazon.DynamoDBv2.DataModel;
var client = new AmazonDynamoDBClient();
var context = new DynamoDBContext(client);
var item = new Item
{
Id = 4,
Type = "Fish",
Name = "Goldie"
};
Version v2.0.0
51
AWS SDK for .NET Developer Guide
Programming Models