开发者

Recover from problem in Linq submit

开发者 https://www.devze.com 2023-02-07 05:28 出处:网络
If I change the values of a Linq class and then try to submit the changes to the database and get a conflict like string is to long or value can not开发者_如何学Go be null, the database will not be ch

If I change the values of a Linq class and then try to submit the changes to the database and get a conflict like string is to long or value can not开发者_如何学Go be null, the database will not be changes. But the question is what's the best way is to recover from this commit problem and role back the values in the class to the unchanged values.


Linq to SQL creates a transaction for each query you execute, which will rollback on failure. If you'd like more control over the transaction, like say if you want to perform many operations on your own, you can create a TransactionScope around your queries.


I would think more in terms of how do you prevent the error from occuring in the first place. If you know there is a possibility that a submit will fail due to a string length, or null value, you should be checking for this prior to committing the records.

Remember, an exception is exactly that -- an exception to something you didn't think about when you were developing the solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消