SaveChanges with Exception looses changes

4 posts, 1 answers
  1. Stuart
    Stuart avatar
    2 posts
    Member since:
    Sep 2014

    Posted 16 Sep 2014 Link to this post

    Hi,

    I have two issues with this product. And im finding it hard to see why (if they are) the observed functionality would have been implemented.

    Issue 1:
    Say i have a VERY basic table with the columns: CustomerID (int PK not null) and CustomerName (varchar not null)
    And a very simple form consisting of a Textbox for the name, a BindingSource for the object and a button to save.


    If i leave the name blank, and click save i quite rightly get the error saying that Null cannot be inserted in to the NAme field. Then, if i populate the name and click save, the code passes SaveChanges but nothing is written.

    If i close and re-open the form, fill in name from the get go and click Save all is well.

    So, i did a little digging and it seems that if an exception is thrown in the SaveChanges method, then the objects which were being tracked are no longer being tracked (I have to call Context.Add()) again to get them to save.


    Is this normal?



  2. Answer
    Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 19 Sep 2014 Link to this post

    Hi Stuart,

    Yes, this is the expected behavior. When SaveChanges() method throws exception, the transaction is rolled-back and everything which is added in the context will be removed. So in your case if the transaction failed, you should add all objects again in the context and execute SaveChanges().

    I hope that helps. If any other questions arise do not hesitate to contact us again.

    Regards,
    Boris Georgiev
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
  3. DevCraft banner
  4. Stuart
    Stuart avatar
    2 posts
    Member since:
    Sep 2014

    Posted 19 Sep 2014 in reply to Boris Georgiev Link to this post

    Hi, Thanks for getting back to me and sorry for wasting your time but i couldnt find any mention of this behavior in the documentation.

    I assumed the context would behave in a similar way to LINQ to SQL.

    This may make migration from LINQ to SQL a little trickier as we haven't had to re-add failed saves but the addition of set based operations like UpdateAll are a life saver.

    Again thanks for getting back to me and if you could point me to the correct place in the documentation that explains this i would be very grateful.
  5. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 22 Sep 2014 Link to this post

    Hi Stuart,

    Thank you for getting back to us.

    You can kindly find the details about the transaction handling executed by Telerik Data Access in this documentation article.

    I hope this helps. If you need further information, do not hesitate to get back to us.



    Regards,
    Doroteya
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
Back to Top