Class SortKeyAttribute
Declares the sort key of an entity. On the type it takes a key template such as ORDER#{CreatedAt}#{OrderId}, stored in an
attribute named sk. On a property it takes no template and declares a plain key: the property's own attribute is the key.
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class SortKeyAttribute : Attribute
- Inheritance
-
SortKeyAttribute
- Inherited Members
Remarks
DynamoDB compares string keys by their UTF-8 bytes. Numbers therefore need a fixed format such as {Id:d10} to sort
numerically, and a template that has no placeholders, such as PROFILE, is legal. The template syntax is described on
PartitionKeyAttribute, and so is the format raw, with what it lifts and what it does not.
A timestamp is written in UTC: a DateTime of kind Local is converted, and one of kind Unspecified, which new DateTime(2026, 1, 15) and default have, names no instant, so the write throws KeyFormatException.
Give it Utc, or use a DateOnly, which has no kind and is written as yyyy-MM-dd. A timestamp takes yyyy, yyyy-MM, yyyy-MM-dd, yyyy-MM-ddTHH, yyyy-MM-ddTHH:mm, s, ms, us or ticks. A bare
{At} is ticks, seven fraction digits as in the attribute: it is the only lossless format, so a read can restore the property from the key. Every other format truncates, and
{At:ms} is the one that matches keys the AWS SDK for .NET wrote.
Constructors
SortKeyAttribute()
Initializes a new instance of the SortKeyAttribute class, for a plain key on a property.
public SortKeyAttribute()
SortKeyAttribute(string)
Initializes a new instance of the SortKeyAttribute class, for a key template on a type.
public SortKeyAttribute(string template)
Parameters
templatestringThe key template, for example
ORDER#{CreatedAt}#{OrderId}.
Properties
AttributeName
Gets or sets the name of the DynamoDB attribute. The default is sk for a template; for a plain key it is the
attribute name of the property, and setting it here is the same as DynamoDbAttributeAttribute.
public string? AttributeName { get; set; }
Property Value
Template
Gets the key template, or null for a plain key.
public string? Template { get; }