Exit transactions if record exists

2 posts, 0 answers
  1. Duncan
    Duncan avatar
    93 posts
    Member since:
    Jul 2012

    Posted 10 Oct 2011 Link to this post

    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?

  2. Alexander
    Alexander avatar
    727 posts

    Posted 12 Oct 2011 Link to this post

    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,
    the Telerik team

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

Back to Top