Amazon DynamoDB · Create
Create one record, in either design
The smallest create, in the two designs DynamoDB tables are made in: an entity with a table of its own, then a customer and an order sharing one table through key templates.
Set up for this page
The scenarios build on the sample application: SampleContext.cs (the dbContext every sample is given); SampleOutput.cs (Step and Show, which print); SampleContext.Application.cs (dbContext.Service, the application's ARX service); SampleModel.cs (the DynamoDB layout and the schema catalog); SampleContext.Direct.cs (dbContext.Direct); DynamoDirectHelper.cs (the checks behind dbContext.Direct).
dbContext.Direct is the samples' own checking code on the plain driver, not part of ARX: leave it out of your application.
1 A table of its own
A product in its own table, keyed by its SKU.
Already in scope: client (IAmazonDynamoDB)
var model = ArxDynamoModel.Create()
.Table("product", partitionKey: "sku")
.Build();
var service = new ArxDynamoDbService(client, model);
var product = new ArxEntity("product");
product["sku"] = "SKU-D101";
product["name"] = "Trail running shoe";
product["price"] = 129.90m;
await service.CreateAsync(product);
Console.WriteLine("created product SKU-D101");Check it in the database
A PartiQL statement: run it in NoSQL Workbench, or with aws dynamodb execute-statement --statement, after the code has run.
SELECT * FROM "product" WHERE "sku" = 'SKU-D101'Expect: 1 item, name "Trail running shoe", price 129.9.
2 One table for customers and orders
A customer and its first order in one table: ARX writes PK, SK and entity_type from the model's templates.
Already in scope: service (ArxDynamoDbService)
var customer = new ArxEntity("customer");
customer["customer_id"] = "C-D101";
customer["name"] = "Northwind Traders";
ArxRecordId id = await service.CreateAsync(customer);
var order = new ArxEntity("sales_order");
order["customer_id"] = "C-D101";
order["order_number"] = "SO-D101";
order["status"] = "OPEN";
order["ordered_on"] = "2026-11-09";
order["total"] = 250m;
await service.CreateAsync(order);
Console.WriteLine($"created customer {id.AsString()} and order SO-D101");Check it in the database
A PartiQL statement: run it in NoSQL Workbench, or with aws dynamodb execute-statement --statement, after the code has run.
SELECT * FROM "crm" WHERE "PK" = 'CUSTOMER#C-D101'Expect: 2 items: SK "ORDER#SO-D101" (entity_type sales_order, GSI1PK "STATUS#OPEN") and SK "PROFILE" (entity_type customer).
Good to know
The model says where each entity goes; the code that creates it is the same in both designs. DynamoDB generates no ids: the id is one of the values you give. A create never overwrites: an item with the same key is ARX3001.
Run the complete sample
The code above comes from one program, built in a fresh project and run against the database. To run it yourself, here is every file and step.
The program, its files and how to run it
Run it locally
- .NET SDK: .NET SDK 10.0.100 or later 10.0.x (target
net10.0). - Create the project:
Terminal dotnet new console -n ArxSample -f net10.0 cd ArxSample - Install the packages:
Terminal dotnet add package ARX.SDK.DynamoDb --version 1.0.0-rc.1 - Database: a Amazon DynamoDB, or DynamoDB Local for development (tested with DynamoDB Local 2.5.2) database with the sample schema.
- Configuration: set
ARX_SAMPLES_DYNAMODBto its connection string. Nothing is hard-coded. - Source code: add the files of the sample application this sample uses (
SampleContext.cs,SampleOutput.cs,SampleContext.Application.cs,SampleModel.cs,SampleContext.Direct.cs,DynamoDirectHelper.cs), then replaceProgram.csand add the other file below, with the file names shown. - Run it:
Terminal dotnet run - Expected output: below.
The files
Runs the sample. Generated for this page; it reads the connection string from the environment and first remakes the sample database from sample-schema.json.
using ArxSamples.DynamoDb;
using ArxSamples.DynamoDb.Operations.Create.Basics;
// The connection string comes from the environment, never from the code (see "Configure the connection").
var connectionString = Environment.GetEnvironmentVariable("ARX_SAMPLES_DYNAMODB")
?? throw new InvalidOperationException("Set the ARX_SAMPLES_DYNAMODB environment variable to your Amazon DynamoDB connection string.");
var dbContext = new SampleContext(connectionString);
// Every run starts from the sample data: this deletes and makes again what sample-schema.json lists
// (save the file in the project folder, see Get started).
await dbContext.Direct.ResetAsync();
await new Basic1_CreateOneRecord().RunAsync(dbContext);
The sample, as it is in the repository.
using Amazon.DynamoDBv2;
using ARX.SDK.DynamoDb.Model;
using ARX.SDK.DynamoDb.Services;
using ARX.SDK.Services;
using static ArxSamples.DynamoDb.SampleOutput;
namespace ArxSamples.DynamoDb.Operations.Create.Basics;
/// <summary>
/// The smallest create, in the two designs DynamoDB tables are made in: an entity with a table of its own, then a
/// customer and an order sharing one table through key templates.
/// </summary>
/// <remarks>
/// The model says where each entity goes; the code that creates it is the same in both designs. DynamoDB generates no
/// ids: the id is one of the values you give. A create never overwrites: an item with the same key is ARX3001.
/// </remarks>
public sealed class Basic1_CreateOneRecord : ISample
{
public string Name => "create-basics-1-create-one-record";
public string Title => "Create one record, in either design";
public async Task RunAsync(SampleContext dbContext)
{
await Scenario1_ATableOfItsOwnAsync(dbContext.Client);
await Scenario2_OneTableForCustomersAndOrdersAsync(dbContext);
}
/// <summary>1. A product in its own table, keyed by its SKU.</summary>
private static async Task Scenario1_ATableOfItsOwnAsync(IAmazonDynamoDB client)
{
Step("1. A table of its own");
var model = ArxDynamoModel.Create()
.Table("product", partitionKey: "sku")
.Build();
var service = new ArxDynamoDbService(client, model);
var product = new ArxEntity("product");
product["sku"] = "SKU-D101";
product["name"] = "Trail running shoe";
product["price"] = 129.90m;
await service.CreateAsync(product);
Show("created product SKU-D101");
/* ===========================================================
* [DIRECT QUERY CHECK]
* -----------------------------------------------------------
* SELECT * FROM "product" WHERE "sku" = 'SKU-D101'
* -- Expect: 1 item, name "Trail running shoe", price 129.9.
* ===========================================================
*/
}
/// <summary>2. A customer and its first order in one table: ARX writes PK, SK and entity_type from the model's templates.</summary>
private static async Task Scenario2_OneTableForCustomersAndOrdersAsync(SampleContext dbContext)
{
Step("2. One table for customers and orders");
var service = dbContext.Service;
var customer = new ArxEntity("customer");
customer["customer_id"] = "C-D101";
customer["name"] = "Northwind Traders";
ArxRecordId id = await service.CreateAsync(customer);
var order = new ArxEntity("sales_order");
order["customer_id"] = "C-D101";
order["order_number"] = "SO-D101";
order["status"] = "OPEN";
order["ordered_on"] = "2026-11-09";
order["total"] = 250m;
await service.CreateAsync(order);
Show($"created customer {id.AsString()} and order SO-D101");
/* ===========================================================
* [DIRECT QUERY CHECK]
* -----------------------------------------------------------
* SELECT * FROM "crm" WHERE "PK" = 'CUSTOMER#C-D101'
* -- Expect: 2 items: SK "ORDER#SO-D101" (entity_type sales_order, GSI1PK "STATUS#OPEN") and SK "PROFILE" (entity_type customer).
* ===========================================================
*/
}
}
Expected output
What dotnet run printed when the sample was verified, on a freshly created sample schema. Ids differ when the tables already hold rows.
> 1. A table of its own
created product SKU-D101
> 2. One table for customers and orders
created customer C-D101 and order SO-D101