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
Performance and Tuning Controlling How Entities Load Controlling How Entity Data Loads Intercepting Queries Miscellaneous Performance and Tuning Improvements Understanding Named Aggregates Bulk Updates and Deletes Batching Caching Database Hints Measuring Performance

Bulk Updates and Deletes

The normal cycle for working with an existing database row begins by loading it as an entity.  You then modify or remove the entity, and save the changes to the database.  This results in an UPDATE or DELETE statement for each affected row.

In some bulk operations, the same change is being applied to all affected rows, and no entity operations are required.  In this case, loading each row as an entity, then sending a statement per row, is not efficient.  It would be more efficient to send a single UPDATE or DELETE with an expression indicating which rows to apply to it.

This can be done using the IUnitOfWork bulk Update and Remove methods.

Bulk Updates

IUnitOfWork.Update takes a query specifying which rows to update, and an object or dictionary specifying the values to update those rows with.  If you pass an object, then LightSpeed uses the names and values of its properties to compose the update.  If you pass a dictionary, then LightSpeed uses the string keys of the dictionary and their associated values.

The following example uses an anonymous type to update the IsBadPaymentRisk column.

Performing a bulk update using a change object

DateTime overdueDate = DateTime.Now.AddMonths(-6);
Query overdueAccounts = new Query(typeof(Customer),
  Entity.Attribute("Balance") < 0 && Entity.Attribute("LastPaymentDate") < overdueDate);
unitOfWork.Update(overdueAccounts, new { IsBadPaymentRisk = true });
unitOfWork.SaveChanges();

The following example uses a dictionary to perform the same update.

Performing a bulk update using a change dictionary

DateTime overdueDate = DateTime.Now.AddMonths(-6);
Query overdueAccounts = new Query(typeof(Customer),
  Entity.Attribute("Balance") < 0 && Entity.Attribute("LastPaymentDate") < overdueDate);
Dictionary<string, object> changes = new Dictionary<string, object>();
changes.Add("IsBadPaymentRisk", true);
unitOfWork.Update(overdueAccounts, changes);
unitOfWork.SaveChanges();

Note that like entity operations, bulk operations are not applied to the database immediately.  You must still call SaveChanges.  This allows bulk updates to be carried out atomically and to be coordinated with entity operations.

Bulk Deletes

IUnitOfWork.Remove takes a query specifying which rows to delete.

Performing a bulk delete

DateTime expiryDate = DateTime.Now.AddMonths(-6);
Query expiredUsers = new Query(typeof(User),
  Entity.Attribute("LastActiveDate") < expiryDate);
unitOfWork.Remove(expiredUsers);
unitOfWork.SaveChanges();

As noted under Bulk Updates, you must remember to call SaveChanges to commit the bulk delete.

To perform this type of query using LINQ you can use the .Remove() extension method in the Mindscape.LightSpeed.Linq namespace to indicate that the query should be a remove operation. As with IUnitOfWork.Remove you will need to call SaveChanges to commit the bulk delete.

Considerations for Bulk Operations

Bulk operations bypass the identity map.  After performing a bulk operation, dispose the unit of work, or at least reset it (by passing true to SaveChanges).

Bulk operations bypass the cache, and do not update the full text search index.  Avoid using bulk updates if your application uses either of these features.

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