How to enforce unique constraint with LINQ to SQL

佐手、 提交于 2020-01-01 19:26:31

问题


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.


回答1:


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().



来源:https://stackoverflow.com/questions/4014482/how-to-enforce-unique-constraint-with-linq-to-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!