Checking For Existing Records On Save

Posts   
 
    
Maxus
User
Posts: 76
Joined: 04-Aug-2006
# Posted on: 14-May-2007 07:24:10   

Hi People,

just a simple one, I want to save an entity to the DB that must be unique, for example:

TestEntity ---> Field1 ---> Field2

On save I want to check if an entity already exists with the same values for field1 and field2 as the one I'm saving that isn't the one im saving, as this is a bussiness wide rule, I thought it would be really cool to do it in the validator class in the ValidateEntityBeforeSave method, is that the right place to do it?

Thanks! Alex

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 14-May-2007 08:33:32   

IMHO, it's better to have a unique constraint on the database. And you might just save and catch an exception in the case of a similar record.

This will be only one hit to the database rather than 2 to check for an existing record before saving.