Updating a Single Column in Linq to SQL using an Alias

This answer on Stack Overflow shows another way to update specific columns of a database record in Linq to SQL: Map the table in the .dbml file a second time, but omit the long columns.

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

This generates the following SQL statements:

SELECT TOP (1) [t0].[OID], [t0].[ID]
FROM [dbo].[FooMax] AS [t0]
WHERE [t0].[OID] = @p0

UPDATE [dbo].[FooMax]
SET [ID] = @p2
WHERE ([OID] = @p0) AND ([ID] = @p1)

One Response to Updating a Single Column in Linq to SQL using an Alias

  1. […] example, using the FooMax table of one of my previous […]

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: