我在我的另一个问题中了解了如何使用 TransactionScope 在 Entity Framework 中配置事务。然而它仍然让我困惑!我的意思是 RIA 服务如何执行事务以及我们如何指定事务选项?我的意思是,假设在 Silverlight 客户端上我们指定了类似这样的内容:-
someContext.Add(someEntity1);
someContext.Add(someEntity2);
someContext.Add(someEntity3);
现在,当我调用 someContext.SubmitChanges()
时,这将在我的域服务类中的服务器上调用 InsertSomeEntity()
。如何保证所有三条记录都插入数据库,如果其中一条记录失败,则所有记录都失败?我们如何更改这些选项?
最佳答案
Chand 的链接有一个很好的例子。 WCF RIA 将提交包含所有 3 个添加的 SubmitChanges 的变更集。在 DomainService 中,您可以重写 PersistChanges 方法来完成事务。
public class SomeEntityDomainService : DomainService
{
SomeEFContext _someEFContext;
public SomeEntityDomainService()
{
_someEFContext = new SomeEFContext();
}
public void InsertSomeEntity(SomeEntity someEntity)
{
// Called 3 times in your example
_someEFContext.SomeEntities.Add(someEntity);
}
protected override bool PersistChangeSet()
{
// Called exactly once per SubmitChanges() in Silverlight
_someEFContext.SaveChanges();
}
}
所有这些都发生在从客户端到服务器的一次请求中,而不是 3 个请求中。
关于silverlight - ria 服务如何管理事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5442644/