views:

90

answers:

2

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!

A: 

You might want to look at this How-To on creating transactional services to make sure that you've completely specified the transaction semantics. Some of the code for that is missing from your question. You might also consider setting up tracing, as in this article, to get more information.

tvanfosson
Yes, you're right I do need to add the transaction flow stuff to my service. Seems I have a deeper problem though, please see: http://stackoverflow.com/questions/2840896/cant-update-rows-in-my-database-using-entity-framework
Dissonant
A: 

Does it add the rows when you remove the TransactionScope?

Dan