views:

49

answers:

3

using Nhibernate;

I'm trying to insert several values a on table which has an unique index on some columns.

I'd like to know if a particular insert raises an exception for having violated the unique constraint.

So, which particular exception type should i catch? I only want to catch this particular one and let all others go up.

Thanks a lot!

+2  A: 

I'm afraid that there is no special exception for that. You will have to catch ADO NET exceptions and look on the inner exception text.

IMHO your approach is not the more appropriate. You should query the DB in order to check BEFORE the insert if the data will violate the unique constraint. If it does, then you don't insert the record.

Claudio Redi
I apreciate your suggestion, but as the data is pretty large i'm trying to go to the DB as little as posible.
mcabral
A: 

NHibernate.NonUniqueObjectException

mcabral
"This exception is thrown when an operation would break session-scoped identity. This occurs if the user tries to associate two different instances of the same class with a particular identifier, in the scope of a single Session." Doesn't seem to be what you need
Claudio Redi
@Claudio you are correct
mcabral