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

SaveChanges with Exception looses changes

3 Answers 43 Views
Data Access Free Edition
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Stuart
Top achievements
Rank 1
Stuart asked on 16 Sep 2014, 03:40 PM
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?



3 Answers, 1 is accepted

Sort by
0
Accepted
Boris Georgiev
Telerik team
answered on 19 Sep 2014, 04:21 PM
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.
 
0
Stuart
Top achievements
Rank 1
answered on 19 Sep 2014, 11:01 PM
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.
0
Doroteya
Telerik team
answered on 22 Sep 2014, 02:21 PM
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.
 
Tags
Data Access Free Edition
Asked by
Stuart
Top achievements
Rank 1
Answers by
Boris Georgiev
Telerik team
Stuart
Top achievements
Rank 1
Doroteya
Telerik team
Share this question
or