Class PartitionKeyAttribute
Declares the partition key of an entity. On the type it takes a key template such as USER#{UserId}, stored in an
attribute named pk. 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 PartitionKeyAttribute : Attribute
- Inheritance
-
PartitionKeyAttribute
- Inherited Members
Remarks
Literals in a template are printable ASCII, {{ and }} are literal braces, and a placeholder is
{Property} or {Property:format}. A variable-width value that contains a character next to a placeholder throws
KeyFormatException, because 1#ORDER in USER#{Id} could address another item. The format raw
lets them through for a string that is the last thing in the template, such as appointment#{Key:raw}, where they can
address nothing else. DynamoDB keeps a partition key for the life of the item, so a template input cannot be updated.
A property that only exists to feed a key takes KeyOnlyAttribute, as it does for [PartitionKey("appointment#{Key:raw}", AttributeName = "Id")] over a plain Key: an input of a template is
stored as an attribute of its own as well, so without it every item gets an extra Key, and nothing says so.
raw lifts the delimiter check and nothing else. An empty value, a lone surrogate, a control character and U+10FFFF still throw KeyFormatException, on a write and through Keys.Of on a read, so
Keys.TryOf is the tool for a value from outside. A stored key that holds one does not fit the template, and a read that restores an input from it throws DynamoDbMappingException.
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
PartitionKeyAttribute()
Initializes a new instance of the PartitionKeyAttribute class, for a plain key on a property.
public PartitionKeyAttribute()
PartitionKeyAttribute(string)
Initializes a new instance of the PartitionKeyAttribute class, for a key template on a type.
public PartitionKeyAttribute(string template)
Parameters
templatestringThe key template, for example
USER#{UserId}.
Properties
AttributeName
Gets or sets the name of the DynamoDB attribute. The default is pk 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; }