Book Image

WCF Multi-layer Services Development with Entity Framework - Fourth Edition

By : Mike Liu
Book Image

WCF Multi-layer Services Development with Entity Framework - Fourth Edition

By: Mike Liu

Overview of this book

Table of Contents (20 chapters)
WCF Multi-layer Services Development with Entity Framework Fourth Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
2
Hosting the HelloWorld WCF Service
Index

Transaction support


In the previous section, we learned that simultaneous changes by different users can be handled by using a version column or the Concurrency Mode property. Sometimes, the same user might have made several changes and some of the changes might not succeed. In this case, we need a way to control the behavior of the overall update result. This is handled by transaction support.

LINQ to Entities uses the same transaction mechanism as ADO.NET, that is, it uses implicit or explicit transactions.

Implicit transactions

By default, LINQ to Entities uses an implicit transaction for each SaveChanges call. All updates between two SaveChanges calls are wrapped within one transaction.

For example, in the following code, we are trying to update two products. The second update will fail due to a constraint. However, as the first update is in a separate transaction, it has been saved to the database and it will stay in the database:

static void TestImplicitTransaction()
{
  using(var NWEntities...