TCM
TCM

Reputation: 16900

How ria services manage transactions

I learnt how can we configure transactions in Entity Framework using TransactionScope in one other question of mine. However it still confuses me! I mean how does RIA services execute transactions and how can we specify transaction options? I mean, suppose on the client in Silverlight we specify something like this :-

someContext.Add(someEntity1);
someContext.Add(someEntity2);
someContext.Add(someEntity3);

Now when i call someContext.SubmitChanges() this is going to call InsertSomeEntity() on the server in my domain service class. What is the guarantee that all three records will be inserted into the database and if one fails all of them fails? And how can we change these options?

Upvotes: 2

Views: 1395

Answers (1)

Ed Chapel
Ed Chapel

Reputation: 6932

Chand's link has a good example. WCF RIA will submit a ChangeSet for the SubmitChanges containing all 3 Add's. In your DomainService, you can override the PersistChanges method to complete the transaction.

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();
    }
}

All of this happens in one request from the client to the server, not 3 requests.

Upvotes: 5

Related Questions