We have a Silverlight application that uses WCF Data Services. We want to add logging functionality: when a new row is generated, the primary key for this new row is also recorded in the logging table. The row generation and the logging should occur within the same transaction. The primary keys are generated via the database (using the IDENTITY keyword).
This might best be illustrated with an example. Here, I create a new Customer row, and in the same transaction I write the Customer's primary key to an AuditLog row. This example uses a thick client and the Entity Framework:
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();
}
It's a trivial problem when developing a thick client using Entity Framework.
However, using Silverlight and ADO.NET data services:
- SaveChanges can only be invoked asynchronously
- I'm not sure TransactionScope is available
- I'm not sure if generated keys can be reflected in the client Edit: According to Alex James they are indeed reflected in the client
So, will this even be possible?