2016-11-11 2 views
0

私はC#で新しく、TransactionScopeの動作を試しています。ここに私のコードがある、と私のトランザクションがロールバックされませんでしたなぜ私は不思議:c#TransactionScopeはロールバックしませんでした

string file1 = "txf1.txt"; 
string file2 = "txf2.txt"; 
using (StreamWriter sw = File.CreateText(file1)) 
{ 
    sw.WriteLine("Hello World"); 
} 

using (StreamWriter sw = File.CreateText(file2)) 
{ 
    sw.WriteLine("Hello World"); 
} 

using (TransactionScope scope = new TransactionScope()) 
{ 
    File.AppendAllText(file1, "Transaktion"); 
    scope.Complete(); 
} 

using (TransactionScope scope = new TransactionScope()) 
{ 
    File.AppendAllText(file2, "Transaktion"); 
    //should roll back the file, but doesn't 
} 

答えて

1

トランザクションマネージャはFileではありません、それは「ソフトウェア取引」ではありません。 TransactionScopeのほぼ100%は、Entity FrameworkやDapperなどのADO.NETやその上に構築されたライブラリと組み合わせて使用​​されます。詳細はMSDN TransactionScope Classをご覧ください。

関連する問題