LINQ to SQL : Update data through Object Model

LINQ to SQL : Update data through Object Model

Rate This
  • Comments 14

LINQ to SQL support updating data through object. Continuing with my previous blog on INSERT, let me discuss about the update method


static void Main(string[] args)


    string strConnection = @"Connection String";


    TestDB db = new TestDB(strConnection);


    //Here I am finding the employee with Id 19

    var updateQ = db.Emps.First(e => e.Id == 19);


    //Then I will modify that employee name and give new name

    updateQ.Name = "Updated Employee";


    //Commit the changes to database

    //at this point DML gets generated



    //To view the updated data




All the methods are coming from DataContext class (responsible for SQL query generation). The above method converts the object addition to DML query.



Leave a Comment
  • Please add 3 and 1 and type the answer here:
  • Post
  • LINQ to SQL support updating data through object. Continuing with my previous blog on INSERT , let me

  • Thanks for the useful post. For me, there are still two questions:

     - What should I do if I change two objects o1 and o2,  want to persist o1 with database but do not want to   submit the changes made to o2?

     - What should I do if I have a distributed architecture and my objects pass remoting or WCF boundaries and their changed forms are sent back to the server-side? Is Linq2SQL able to find the changes and submit them to database?

  • By default LINQ to SQL uses optimistic concurrency and you could implement the pessimistic concurrency by implementing the TransactionScope which is new in .NET Framework 3.0.

    If the value changes in the database and you want to update the changed data, LINQ to SQL will throw you an error.


  • Well one clue to what I want to do -- DML query..?

  • What is the equivalent in VB.NET?

  • "TransactionScope which is new in .NET Framework 3.0."

    TransactionScope was in 2.0 :D

  • Nice post.Really helped a lot.Thanks a lot.

  • Thanks

    an answer to the equivalent in VB :same :)

    try it not so different..

  • Thanks for your helpful tip.

    Can i translate this lecture to my blog?

  • @song,

    Please feel free to do.

  • I did same as this, But it did not work. I don't know why. Can somebody help me fix it please?

    my code is:

    using (DataContext.Order db = new DataContext.Order((string)HttpContext.Current.Session["Level"]))


                   var NewOrder = db.Users.Single(p=>p.Id==intId); // find update record

                   NewOrder.Name = txtName.Text.Trim();

                   NewOrder.Address = txtAdd.Text.Trim();

                   NewOrder.Zip = txtZip.Text.Trim();

                   db.SubmitChanges();                                //submit update

                   stMsg.Text = "Record Updated.";


  • The solution presented here does not work.

    I don't understand why bother wrriting a solution that does not work.

  • Hi vali... the solution works fine.

  • Awesome clear

Page 1 of 1 (14 items)