Mindscape
  • Register
  • Login
  • YOUR CART IS EMPTY
home
Software
  • Developers Toolbox
  • LightSpeed
  • Raygun
  • WPF Elements
  • Web Workbench
  • Metro Elements
  • Silverlight Elements
  • NHibernate Designer
  • Phone Elements
  • WPF Diagrams
  • Simple DB management
  • Visual Tools for SharePoint
Downloads
Store
  • Buy online
  • Purchase order
  • Volume discounts
  • Reseller
  • Software license
  • Contact sales
Blog
Support
About
  • About Us
  • Contact
  • Testimonials
rss
twitter
facebook
Basic Operations Working with Entities LightSpeed Configuration Basics Querying the Database Using LINQ Querying the Database Using Query Objects Creating Modifying and Deleting Entities Transactions

Transactions

SaveChanges is automatically transactional: that is, if more than one entity needs to be saved, LightSpeed guarantees that the changes to the database will be atomic and durable (provided the database supports transactions).  To achieve this, SaveChanges automatically begins a transaction before sending the first change, and commits it after sending the last change if all changes have been successful.

As far as LightSpeed is concerned, however, each SaveChanges is an independent transaction.  If you need multiple LightSpeed operations to be part of a single transaction, you must specify that transaction yourself.

Using TransactionScope

The easiest way to control transactions with LightSpeed is to use the .NET TransactionScope class.  To do this, simply surround any calls to IUnitOfWork with the standard TransactionScope block:

Using a system transaction with a LightSpeed unit of work

using (var transactionScope = new TransactionScope())
{
  using (var unitOfWork = _context.CreateUnitOfWork())
  {
    var contribution = unitOfWork.FindById<Contribution>(1);
    contribution.Description = "A description";
    unitOfWork.SaveChanges();
  }
  transactionScope.Complete();
}

Using ADO.NET Transactions

However, not all data providers support TransactionScope.  (Specifically, at the time of writing, Oracle and PostgreSQL do not.)

For these cases, IUnitOfWork exposes a BeginTransaction method. This method returns a standard ADO.NET IDbTransaction object. In this case, the usage pattern is slightly different as we need to flush changes using the IUnitOfWork.SaveChanges method before committing the transaction and then completing the unit of work.

Using an ADO.NET transaction with a LightSpeed unit of work

using (var unitOfWork = _context.CreateUnitOfWork())
{
  using (var transaction = unitOfWork.BeginTransaction())
  {
    var contribution = unitOfWork.FindById<Contribution>(1);
    contribution.Description = "A description";
    unitOfWork.SaveChanges();
    transaction.Commit();
  }
}

Automatic Transactions

Remember that you only need to manually specify a transaction if you need to coordinate at a larger scope than a single SaveChanges.  LightSpeed automatically ensures that all database flush operations run within a transaction.  In most cases therefore you will not need to create transactions explicitly.

Data Products

  • LightSpeed ORM
  • NHibernate Designer
  • SimpleDB Tools
  • SharePoint Tools

DevOp Tools

  • Raygun

Visual Controls

  • WPF Elements
  • WPF Diagrams
  • Silverlight Elements
  • Phone Elements

Popular Products

  • Web Workbench

    Modern web development for Visual Studio!

  • Mindscape Megapack

    All Mindscape products for one awesome price!

Quick Links

  • Forums
  • Blog
  • Register
  • Login
  • Contact us
  • Twitter
  • Facebook
  • Google+
  • YouTube
  • Linkedin
  • Rss

© Mindscape 2025. Mindscape is a registered trademark of Mindscape Limited.

  • Terms
  • Privacy