数据库不使用 TransactionScope 更新

发布于 2024-09-01 07:37:06 字数 1239 浏览 8 评论 0原文

我有一个客户端尝试以事务方式与 WCF 服务进行通信。客户端将一些数据传递给服务,服务相应地将数据添加到其数据库中。由于某种原因,服务提交到数据库的新数据没有被持久化。当我查看服务器资源管理器中的表数据时,没有添加新行...

相关代码片段如下:

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

注意:我正在使用实体将对象持久保存到数据库的框架。

[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() 的所有组合,但新的付款数据没有添加到数据库中!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月静好 2024-09-08 07:37:06

您可能需要查看有关创建事务服务的操作方法以确保您已完全指定事务语义。您的问题中缺少一些代码。您还可以考虑设置跟踪,如这篇文章中所示,以获取更多信息。

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.

℡寂寞咖啡 2024-09-08 07:37:06

当您删除 TransactionScope 时它会添加行吗?

Does it add the rows when you remove the TransactionScope?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文