This is a migrated thread and some comments may be shown as answers.

Exit transactions if record exists

1 Answer 65 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Duncan
Top achievements
Rank 2
Duncan asked on 11 Oct 2011, 12:33 AM
I was wondering if there is a way to exit a transaction or throw a exception if a record with a field witht he same data as what is being added exists in the database.

I know that i could use LINQ to lookup the record, but i was wondering if there was another, better way of doing this?

Duncan

1 Answer, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 12 Oct 2011, 05:17 PM
Hi Duncan,

If you do not want to have duplicate values for particular column in the database, this is normally achieved by using Unique constraints. In this case if you try to insert a record with the same value, an exception would be thrown. However, throwing/catching exceptions is a heavy operation, so I would not recommend you to choose this approach. 
I believe the cleanest way is to execute a short Linq query to check if the value exist before inserting the new object. You can nevertheless define unique constraints on the database level to ensure data integrity, just in case.
I hope that helps.

Kind regards,
Alexander
the Telerik team

Check out the latest stable build of Telerik OpenAccess ORM. Download it and benefit from our new Project Templates.

Tags
General Discussions
Asked by
Duncan
Top achievements
Rank 2
Answers by
Alexander
Telerik team
Share this question
or