Path: redb/redb.Core/Attributes/RedbUniqueAttribute.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Attributes/RedbUniqueAttribute.cs
----
using System;
namespace redb.Core.Attributes
{
///
/// Declares a Props property a unique key within its scheme: no two objects of the scheme may
/// hold the same value in it. Enforced by the database — a unique index over
/// _values (_id_structure, _unique), where _unique is the 128-bit hash of the
/// value's canonical form () — and surfaced as
/// .
///
///
/// Allowed on: a scalar property — string, integers, Guid, bool,
/// double/float, decimal, DateTime/DateTimeOffset/
/// DateOnly, byte[] and their nullable forms — of the Props class or of a nested
/// class reached without crossing a collection (S2); and on a whole nested class, array or
/// dictionary property, where the key is the SUBTREE content (S1). Not on enums (stored as their
/// name in text, not a key type), single RedbObject references, or
/// members of collection ELEMENTS — element rows share one structure across every element of
/// every object. Scheme synchronisation rejects a misplaced attribute with
/// .
///
///
///
/// null never participates: any number of objects may leave the key empty.
///
///
///
/// The value is compared exactly, after Unicode NFC normalisation for strings: no trimming, no
/// case folding. A key is a notion of the application; an application that wants
/// case-insensitive or trimmed keys normalises them before saving — the same contract as a unique
/// index in any database, and what EF Core and Hibernate do.
///
///
///
/// Putting the attribute on a property of a scheme that already has data recomputes the column
/// for every existing object at the next synchronisation and reports duplicates
/// () instead of failing start-up: duplicate rows stay
/// outside the index until they are resolved.
///
///
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class RedbUniqueAttribute : Attribute
{
///
/// On a COLLECTION property switches from the subtree-content key (the default) to
/// ELEMENT keys: - no duplicate elements inside one
/// collection; - element values unique across the whole
/// scheme. Meaningless (and rejected at synchronisation) on scalars and nested classes,
/// whose bare attribute already carries the strongest reading.
///
public UniqueScope Scope { get; set; } = UniqueScope.Default;
}
}