数据库不使用 TransactionScope 更新
我有一个客户端尝试以事务方式与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要查看有关创建事务服务的操作方法以确保您已完全指定事务语义。您的问题中缺少一些代码。您还可以考虑设置跟踪,如这篇文章中所示,以获取更多信息。
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.
当您删除 TransactionScope 时它会添加行吗?
Does it add the rows when you remove the TransactionScope?