Skip to content

UniqueAttribute class

namespace: Serenity.Data.Mapping   assembly: Serenity.Net.Services

Defines a unique constraint on the field. Sets the Unique flag and, by default, makes UniqueFieldSaveBehavior check the constraint before save by running an existence query, so that a user friendly validation error is returned instead of a database exception. The actual database unique index (if any) remains authoritative.

[AttributeUsage(AttributeTargets.Property)]
public class UniqueAttribute : SetFieldFlagsAttribute

Public Members

name description
UniqueAttribute() Initializes a new instance of the UniqueAttribute class.
CheckBeforeSave { get; set; } Gets or sets a value indicating whether to check this constraint before save.
DisableDefaultBehavior { get; set; } Gets or sets a value indicating whether the default UniqueFieldSaveBehavior pre-check should be disabled for this field. Set to true if you want to replace the default behavior with your own.
ErrorMessage { get; set; } Gets or sets the error message.
IgnoreDeleted { get; set; } Gets or sets a value indicating whether to ignore deleted records.
IgnoreNulls { get; set; } Gets or sets a value indicating whether to ignore null values.
Name { get; set; } Gets or sets the name of the constraint. Not used.

Remarks

Set CheckBeforeSave to false to skip the default pre-check, or DisableDefaultBehavior to true to disable the default behavior entirely, e.g. when you will replace it with your own behavior (derive from BaseUniqueConstraintSaveBehavior or implement your own save behavior). The default pre-check query may be unscoped; override BuildUniqueConstraintQuery to add row level scoping (e.g. tenant/owner) so that it matches the actual database constraint.

See Also