Silverlight上的ADO.NET数据服务:在同一事务中使用生成的密钥
我们有一个使用WCF数据服务的Silverlight应用程序。我们希望添加日志记录功能:当生成新行时,该新行的主键也记录在日志记录表中。行生成和日志记录应在同一事务中进行。主键通过数据库生成(使用IDENTITY关键字) 最好用一个例子来说明这一点。在这里,我创建了一个新的Customer行,并在同一事务中将客户的主键写入AuditLog行。此示例使用厚客户端和实体框架:Silverlight上的ADO.NET数据服务:在同一事务中使用生成的密钥,silverlight,transactions,wcf-data-services,astoria,Silverlight,Transactions,Wcf Data Services,Astoria,我们有一个使用WCF数据服务的Silverlight应用程序。我们希望添加日志记录功能:当生成新行时,该新行的主键也记录在日志记录表中。行生成和日志记录应在同一事务中进行。主键通过数据库生成(使用IDENTITY关键字) 最好用一个例子来说明这一点。在这里,我创建了一个新的Customer行,并在同一事务中将客户的主键写入AuditLog行。此示例使用厚客户端和实体框架: using (var ts = new TransactionScope()) { Audi
using (var ts = new TransactionScope())
{
AuditTestEntities entities = new AuditTestEntities();
Customer c = new Customer();
c.CustomerName = "Acme Pty Ltd";
entities.AddToCustomer(c);
Debug.Assert(c.CustomerID == 0);
entities.SaveChanges();
// The EntityFramework automatically updated the customer object
// with the newly generated key
Debug.Assert(c.CustomerID != 0);
AuditLog al = new AuditLog();
al.EntryDateTime = DateTime.Now;
al.Description = string.Format("Created customer with customer id {0}", c.CustomerID);
entities.AddToAuditLog(al);
entities.SaveChanges();
ts.Complete();
}
在使用实体框架开发厚客户机时,这是一个微不足道的问题
但是,使用Silverlight和ADO.NET数据服务:
- 只能调用SaveChanges 异步
- 我不确定TransactionScope是否可用
- 我不确定生成的密钥是否可以反映在客户端中编辑:根据我的判断,它们确实反映在客户端中
那么,这可能吗?简短回答:不,这甚至不可能 好的。。。因此:
// Create and insert customer ...
// Create audit log and relate to un-insert customer
CustomerAuditLog al = new CustomerAuditLog();
al.EntryDateTime = DateTime.Now;
al.Description = string.Format("Created customer with {Customer.ID}");
// assuming your entities implement INotifyPropertyChanging and you are using
// the Data Services Update to .NET 3.5 SP1 to use DataServiceCollection
// to notify the DataServiceContext that a relationship has been formed.
//
// If not you will manually need to tell Astoria about the relationship too.
al.Customer = c;
entities.AddToAuditLog(al);
entities.SaveChanges();
在底层数据源或甚至数据库中有某种逻辑,用适当的值替换{Customer.ID}
您可能能够让它工作,因为如果在同一事务中发生两个插入,并且其中一个(CustomerAuditLog)依赖于另一个(Customer),那么底层数据源应该对它们进行适当的排序
但正如您所看到的,这种方法有点粗糙,您不希望每个可能的审计消息都有一个类型,是吗!而且。。。它甚至可能不起作用
希望这有帮助
微软数据服务团队简短回答:不,这根本不可能 好的。。。因此:
// Create and insert customer ...
// Create audit log and relate to un-insert customer
CustomerAuditLog al = new CustomerAuditLog();
al.EntryDateTime = DateTime.Now;
al.Description = string.Format("Created customer with {Customer.ID}");
// assuming your entities implement INotifyPropertyChanging and you are using
// the Data Services Update to .NET 3.5 SP1 to use DataServiceCollection
// to notify the DataServiceContext that a relationship has been formed.
//
// If not you will manually need to tell Astoria about the relationship too.
al.Customer = c;
entities.AddToAuditLog(al);
entities.SaveChanges();
在底层数据源或甚至数据库中有某种逻辑,用适当的值替换{Customer.ID}
您可能能够让它工作,因为如果在同一事务中发生两个插入,并且其中一个(CustomerAuditLog)依赖于另一个(Customer),那么底层数据源应该对它们进行适当的排序
但正如您所看到的,这种方法有点粗糙,您不希望每个可能的审计消息都有一个类型,是吗!而且。。。它甚至可能不起作用
希望这有帮助
微软数据服务团队