Checking for Unique Values
Although you can add a rule to your business rule object that checks for the uniqueness of a value by seeing if there are any other records in the database with that value, this can cause a race condition in applications where there is heavy traffic. Conceivably, between the time you check if the unique value exists and when you save the new/updated record, another user could have saved a record with the same value.
Ultimately, the only way to ensure that a value is truly unique is to create a UNIQUE constraint on that column in the database and let the data access layer throw an exception. This is also a good approach because you avoid unnecessarily checking for a unique value every time you save a record.
After creating the database constraint, go to your associated business object controller, override the HandleException method and check for the specific unique constraint you created. If you find it, set the business controller's ExceptionHandled property to true, and add an appropriate message to the broken rules collection. For example:
public override void HandleException(Exception e) { if (e.InnerException.Message.Contains("IX_CustomerPhoneUnique")) { // Unique key constraint this.ExceptionHandled = true; this.Rules.AddBrokenRule("Phone number already assigned to another customer"); return; } base.HandleException(e); }
© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 02/12/26
Comment or report problem with topic
