You need to just create a scalar method that checks to see if there is another record with this uniquness. It would look something like this:Public Function AreThereDuplicates(Byval MyTestValue As String) As Boolean
'-- Establish Locals
Dim loCommand As New SqlClient.SqlCommand
Dim lnPK As Integer
'-- Get the primary key
If Me.Count = 0 Then
lnPK = -1
Else
lnPK = Me.MyPrimaryKey
End If
'-- Build the SELECT string
loCommand.CommandText = "SELECT Count(*) FROM MyTable WHERE MyPrimaryKey != @MyPrimaryKey"
loCommand.CommandText &= " AND MyTestField = @MyTestField"
'-- Create the parameters
loCommand.Parameters.Add(New SqlClient.SqlParameter("@MyPrimaryKey", SqlDbType.Int))
loCommand.Parameters.Add(New SqlClient.SqlParameter("@MyTestField", SqlDbType.VarChar))
'-- Set the values
loCommand.Parameters.Item("@MyPrimaryKey").Value = lnPK
loCommand.Parameters.Item("@MyTestField").Value = MyTestValue
'-- Return Results
Return Me.ExecuteScalar(loCommand) > 0
End Function
Then just call this in your business rules when checking (or whenever you need to):
If AreThereDuplicates("TestValue") Then
Me.AddBrokenRule(...)
End If