KB Article A2712
Visible to All Users

How to define a multi-field unique index in a persistent object

Description:
The value combination of several properties in my object must be unique. If I were creating a database, I'd define a unique index against several columns. How to implement a multi-key unique index in XPO?

Answer:
You should use the Indexed attribute with the Unique parameter. It must be applied to one of the properties of your composed index. The names of other properties must be specified delimiting them with semicolons as the first parameter.

C#
using DevExpress.Xpo; public class Person : XPObject { [Indexed("FirstName;BirthDate", Unique=true)] public string LastName = string.Empty; public string FirstName = string.Empty; public DateTime BirthDate; }
Visual Basic
Imports DevExpress.Xpo Public Class Person Inherits XPObject <Indexed("FirstName;BirthDate", Unique:=True)> _ Public LastName As String = String.Empty Public FirstName As String = String.Empty Public BirthDate As DateTime End Class

See Also:
How to add a unique constraint to a property
How to create a persistent object for a database table with a compound key
How to make user-friendly object identifiers

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.