views:

26

answers:

1

Hi,

I'd like to enforce a unique constraint on two columns with LINQ to SQL. I have got a constraint configured on a SQL table, but is there a way to use it with LINQ to SQL?

Unique constraint is applied to two columns, both of which are foreign keys.

Thank you

Edit: I could catch exception, that's if a very specific exception gets thrown. Alternative is to check state of the table before running any updates/inserts. All seems to be like too much work for a very simple task.

+3  A: 

In my opinion, L2S should not be doing the actual enforcement of this. Your database should be doing it via a unique constraint. Then, when you attempt to do an insert or update via L2S, simple catch the exception (if any) being returned when you call SubmitChanges().

Randy Minder
So enforcing unique constraints through middle tier by catching and handling exception is fine?
vikp
@vikp - You are not really enforcing it in the middle tier. The database is enforcing it. You are simply catching any errors that might occur during an update/insert in the middle tier. This is perfectly fine.
Randy Minder