我有一個客戶端嘗試以事務方式與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()的所有組合,但是新的支付數據並沒有被添加到數據庫中!
似乎它與更深層次的問題有關,與TransactionScope無關。請參閱:http://stackoverflow.com/questions/2840896/cant-update-rows-in-my-database-using-entity-framework – Dissonant 2010-05-15 16:44:10