Skip to content

UniqueConstraintAttribute class

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

Adds a unique constraint check to the row. By default UniqueConstraintSaveBehavior checks 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.Class, AllowMultiple = true)]
public class UniqueConstraintAttribute : Attribute

Public Members

name description
UniqueConstraintAttribute(…) Initializes a new instance of the UniqueConstraintAttribute class.
CheckBeforeSave { get; set; } Gets or sets a value indicating whether constraint should be checked before save, default true.
DisableDefaultBehavior { get; set; } Gets or sets a value indicating whether the default UniqueConstraintSaveBehavior pre-check should be disabled for this constraint. Set to true if you want to replace the default behavior with your own.
ErrorMessage { get; set; } Gets or sets the error message.
Fields { get; } Gets the fields.
IgnoreDeleted { get; set; } Gets or sets a value indicating whether to ignore deleted records while checking the constraint.
Name { get; set; } Gets or sets the constraint name. Not used at the moment.

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