2010-05-15 31 views
1

我有一個客戶端嘗試以事務方式與WCF服務進行通信。客戶端將一些數據傳遞給服務,服務會相應地將數據添加到其數據庫中。出於某種原因,服務提交給其數據庫的新數據不會被保留。當我看看在服務器資源管理器沒有添加新行的表數據...數據庫不使用TransactionScope更新

相關的代碼片段低於:

客戶

static void Main() 
{ 
    MyServiceClient client = new MyServiceClient(); 
    Console.WriteLine("Please enter your name:"); 
    string name = Console.ReadLine(); 
    Console.WriteLine("Please enter the amount:"); 
    int amount = int.Parse(Console.ReadLine()); 

    using (TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required)) 
    { 
     client.SubmitData(amount, name); 
     transaction.Complete(); 
    } 

    client.Close(); 
} 

服務

注意:我使用實體框架將對象保存到數據庫。

[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)] 
public void SubmitData(int amount, string name) 
{ 
    DatabaseEntities db = new DatabaseEntities(); 
    Payment payment = new Payment(); 
    payment.Amount = amount; 
    payment.Name = name; 
    db.AddToPayment(payment); //add to Payment table 
    db.SaveChanges(); 
    db.Dispose(); 
} 

我猜它與在客戶端使用的TransactionScope有關。我已經嘗試了db.SaveChanges()和db.AcceptAllChanges()的所有組合,但是新的支付數據並沒有被添加到數據庫中!

+0

似乎它與更深層次的問題有關,與TransactionScope無關。請參閱:http://stackoverflow.com/questions/2840896/cant-update-rows-in-my-database-using-entity-framework – Dissonant 2010-05-15 16:44:10

回答

0

您可能希望查看此How-To創建事務服務以確保您已完全指定事務語義。您的問題中缺少一些代碼。您也可以考慮設置跟蹤,如在此article中獲取更多信息。

+0

是的,你是對的我需要添加事務流的東西給我服務。似乎我有一個更深的問題,但請參閱:http://stackoverflow.com/questions/2840896/cant-update-rows-in-my-database-using-entity-framework – Dissonant 2010-05-15 16:45:23

0

它刪除TransactionScope時添加行嗎?