LINQ to SQL : Update data through Object Model

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

    db.SubmitChanges();

    //To view the updated data

    ObjectDumper.Write(db.Emps);

}

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

Namoskar!!!