Updating a Single Column in Linq to SQL

The standard way to update a record using Linq to SQL is to load a record from the database, assign the new column values, and submit the changes:

using (var database = new DataContext())
{
  var foo = database.Foos.Where(f => f.OID == OID).FirstOrDefault();
  foo.ID = newID;
  database.SubmitChanges();
}

In case you are accessing tables containing n/varchar(max) or varbinary(max) columns (previously n/text and image), such as this:

CREATE TABLE [dbo].[FooMax](
  [OID] [int] IDENTITY(1,1) NOT NULL,
  [ID] [nvarchar](50) NOT NULL,
  [SomeText] [nvarchar](max) NULL,
  [SomeBinary] [varbinary](max) NULL,
  CONSTRAINT [PK_FooMax] PRIMARY KEY CLUSTERED ([OID] ASC)
)

above code means that the whole record is loaded into memory just to update a single column. Bear in mind that n/varchar(max) and varbinary(max) column can hold data up to 2GB each!

This post starts a series of posts presenting solutions to this problem.

4 thoughts on “Updating a Single Column in Linq to SQL

  1. Pingback: Updating a Single Column in Linq to SQL using a Stored Procedure « devioblog

  2. Pingback: Updating a Single Column in Linq to SQL using a View « devioblog

  3. Pingback: Updating a Single Column in Linq to SQL – Summary « devioblog

  4. Pingback: Batch Insert using Linq To SQL (2) « devioblog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.