开发者

Database doesn't update using TransactionScope

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 n开发者_StackOverflow中文版ame:");
    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!


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.


Does it add the rows when you remove the TransactionScope?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜