Class DynamoDbEntityAttribute
Maps a type to a DynamoDB item. The source generator implements the mapping, so the type must be partial, as must its
containing types, and it needs a partition key and a DiscriminatorAttribute.
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Struct, AllowMultiple = false, Inherited = false)]
public sealed class DynamoDbEntityAttribute : Attribute
- Inheritance
-
DynamoDbEntityAttribute
- Inherited Members
Examples
[DynamoDbEntity]
[Discriminator("PRODUCT")]
[PartitionKey("PRODUCT#{Sku}")]
[SortKey("DETAILS")]
public partial record Product
{
public required string Sku { get; init; }
public required string Name { get; init; }
public required decimal Price { get; init; }
}
Remarks
Every public instance property that can be read and that has a setter or a matching constructor parameter becomes an
attribute named like the property. Null values are left out of the item. So is an empty set, which DynamoDB cannot store, and a read gives a missing set back as an empty one, so Tags = [] round-trips.
An empty string, list or map is stored, unless OmitEmptyAttribute leaves it out. The attribute applies to the marked type only: a
derived entity declares its own keys and discriminator, and inherits the mapped properties of its base.
Constructors
DynamoDbEntityAttribute()
public DynamoDbEntityAttribute()
Properties
Table
Gets or sets the name of the table the entity lives in, which Table<T>() opens. It wins over Table of the assembly.
public string? Table { get; set; }
Property Value
Examples
[DynamoDbEntity(Table = "orders")]
[Discriminator("ORDER")]
[PartitionKey("CUSTOMER#{CustomerId}")]
[SortKey("ORDER#{OrderId}")]
public partial record Order
{
public required string CustomerId { get; init; }
public required string OrderId { get; init; }
}
var orders = client.Table<Order>(); // the table "orders", or what TableNaming makes of it
Remarks
An explicit name wins. Table<T>(string) opens the table it is given and ignores the declaration, which is how a test gives each run a table of its own. Only a
declared name goes through TableNaming, which turns appdata into dev-appdata for an environment.
DynamoDB's rules apply. The name has 3 to 255 characters out of a-z, A-Z, 0-9, _, - and .; anything else is
DYNA039 at the attribute. null, the default, declares nothing, and the entity takes the table of the assembly, if it has one.
One table, one key schema. Two entities of a project that declare one table with different names of the partition key or the sort key are DYNA040, and Table<T>() of an entity that declares no table is DYNA238.