frenchie
frenchie

Reputation: 51927

Updating a single column in LINQ to SQL

I have a table with multiple columns and I want to update only one column. I'm writing a method that first fetches the record and then updates it by changing the value of the column. Like this:

using (myDC...)
{
  var recordInDB = (from...
                    where ....
                    select r).SingleOrDefault();

  if (recordInDB != null)
  {
     recordInDB.MyColumn = newValue;

     myDC.SubmitChanges();
  }

Is this going to keep all the other columns as they were and only update the one I want to change or is this going to clear all columns and update the column MyColumn with the new value?

Upvotes: 6

Views: 7889

Answers (2)

ThePaye
ThePaye

Reputation: 1611

This will not change the other columns in your table. Only the one you are updating.

Upvotes: 6

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

This is going to keep the columns that you did not touch the way they are in the database at the time of the read.

On SubmitChanges call LINQ2SQL compares the values in the fields of the object to the values in the database, and updates only the ones that have changed.

Upvotes: 4

Related Questions