transactionscope

Will an inner transaction scope roll back if the outer transaction scope doesn't complete?

送分小仙女□ 提交于 2019-11-28 04:29:10
I have two transaction scopes, one within another. I would love to know if the inner transaction scope will be rolled back after it has been committed and the outer one does not complete. Since they are nested, the inner transaction will roll back. This is not the whole story, and depends on how you create the nested transaction, but by default, it will roll back. This article goes into depth about TransactionScope and should answer most of your questions. Being distributed or not is irrelevant. It depends on the scope option you start the nested transaction scope with. If you use the default

Data committed even though System.Transactions.TransactionScope.Commit() not called

那年仲夏 提交于 2019-11-28 04:17:54
问题 Under what circumstances can code wrapped in a System.Transactions.TransactionScope still commit, even though an exception was thrown and the outermost scope never had commit called? There is a top-level method wrapped in using (var tx = new TransactionScope()) , and that calls methods that also use TransactionScope in the same way. I'm using typed datasets with associated tableadapters. Could it be that the commands in the adapter aren't enlisting for some reason? Do any of you know how one

Stored Procedure without transaction in Entity Framework

烂漫一生 提交于 2019-11-28 01:09:05
I'm calling a stored procedure in Entity Framework 6 that can create Databases and tables if necessary. It is throwing the error; Message "CREATE DATABASE statement not allowed within multi-statement transaction.\r\nALTER DATABASE statement not allowed within multi-statement transaction.\r\nDatabase 'CoreSnapshotJS3' does not exist. Make sure that the name is entered correctly." string I do not want it in a transaction, and have used this to supress the transaction using (var transation = new TransactionScope(TransactionScopeOption.Suppress)) { return ((IObjectContextAdapter)this)

Multiple SaveChanges calls in entity framework

。_饼干妹妹 提交于 2019-11-27 19:57:46
问题 I am building my own custom repository, based on entity framework, and I'm creating some extension methods that allow me to save partial view models as entity models so I'm building my own Add and Update methods. Currently, each method has SaveChanges() from DbContext called at the end which means for every model, one call will be invoked. I'm building this base DAL pattern for MVC4 sites which means most of the time I will access 1 model, but it does not have to be the case though. Is it too

TransactionScope: Avoiding Distributed Transactions

萝らか妹 提交于 2019-11-27 19:26:33
I have a parent object (part of a DAL) that contains, amongst other things, a collection ( List<t> ) of child objects. When I'm saving the object back to the DB, I enter/update the parent, and then loop through each child. For maintainability, I've put all the code for the child into a separate private method. I was going to use standard ADO Transactions, but on my travels, I stumbled across the TransactionScope object, which I believe will enable me to wrap all DB interaction in the parent method (along with all interaction in the child method) in one transaction. So far so good..? So the

One transaction with multiple dbcontexts

守給你的承諾、 提交于 2019-11-27 19:08:12
I am using transactions in my unit tests to roll back changes. The unit test uses a dbcontext, and the service i'm testing uses his own. Both of them are wrapped in one transaction, and one dbcontext is in the block of the other. The thing is, when the inner dbcontext saves his changes, it's not visible to the outer dbcontext (and i don't think it's because the other dbcontext might already have the object loaded). Here is the example: [TestMethod] public void EditDepartmentTest() { using (TransactionScope transaction = new TransactionScope()) { using (MyDbContext db = new MyDbContext()) { /

Refactoring ADO.NET - SqlTransaction vs. TransactionScope

牧云@^-^@ 提交于 2019-11-27 18:46:46
I have "inherited" a little C# method that creates an ADO.NET SqlCommand object and loops over a list of items to be saved to the database (SQL Server 2005). Right now, the traditional SqlConnection/SqlCommand approach is used, and to make sure everything works, the two steps (delete old entries, then insert new ones) are wrapped into an ADO.NET SqlTransaction. using (SqlConnection _con = new SqlConnection(_connectionString)) { using (SqlTransaction _tran = _con.BeginTransaction()) { try { SqlCommand _deleteOld = new SqlCommand(......., _con); _deleteOld.Transaction = _tran; _deleteOld

EF: How do I call SaveChanges twice inside a transaction?

 ̄綄美尐妖づ 提交于 2019-11-27 14:00:44
问题 Using Entity Framework (code first in my case), I have an operation that requires me to call SaveChanges to update one object in the DB, and then SaveChanges again to update another object. (I need the first SaveChanges to resolve an issue where EF can't figure out which object to update first). I tried doing: using (var transaction = new TransactionScope()) { // Do something db.SaveChanges(); // Do something else db.SaveChanges(); tramsaction.Complete(); } When I run that, I get an exception

Common Gotchas when using TransactionScope and MS DTC

亡梦爱人 提交于 2019-11-27 13:19:16
问题 I am just starting to work with using TransactionScope, I find that there are always unexpected things I run into that take forever to debug. I figure that having a consolidated list of these would be great for those "weird error" circumstances, plus to expand our knowledge of oddness in the platform. Some context on how I am going to be using transaction scopes: web application multiple web servers, application servers and sql servers transactions will be mainly database transactions but

TransactionScope With Files In C# [duplicate]

梦想与她 提交于 2019-11-27 13:16:55
This question already has an answer here: How to write a transaction to cover Moving a file and Inserting record in database? 4 answers I've been using TransactionScope to work with the database and it feels nice. What I'm looking for is the following: using(var scope=new TransactionScope()) { // Do something with a few files... scope.Complete(); } but obviously this doesn't work -- if there are 20 files, and an exception occurs on the 9th file, all previous 8 remain changed and the rest unchanged -- no rollback is performed. So, what would be the best way to implement a scope-like behavior