Database doesn't update using TransactionScope
Posted
by Dissonant
on Stack Overflow
See other posts from Stack Overflow
or by Dissonant
Published on 2010-05-15T12:49:44Z
Indexed on
2010/05/15
12:54 UTC
Read the original article
Hit count: 239
I have a client trying to communicate with a WCF service in a transactional manner. The client passes some data to the service and the service adds the data to its database accordingly. For some reason, the new data the service submits to its database isn't being persisted. When I have a look at the table data in the Server Explorer no new rows are added...
Relevant code snippets are below:
Client
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();
}
Service
Note: I'm using Entity Framework to persist objects to the database.
[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();
}
I'm guessing it has something to do with the TransactionScope being used in the client. I've tried all combinations of db.SaveChanges() and db.AcceptAllChanges() as well, but the new payment data just doesn't get added to the database!
© Stack Overflow or respective owner