Welcome to MSDN Blogs Sign in | Join | Help

Wriju's BLOG

.NET and everything
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!!!

Posted: Tuesday, July 17, 2007 12:55 AM by wriju
Filed under: , , , ,

Comments

Noticias externas said:

LINQ to SQL support updating data through object. Continuing with my previous blog on INSERT , let me

# July 16, 2007 9:00 PM

Arash said:

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?

# August 5, 2007 2:39 AM

wriju said:

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.

Wriju

# August 6, 2007 4:37 PM

Travich said:

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

# December 17, 2007 8:53 PM

Borge said:

What is the equivalent in VB.NET?

# March 14, 2008 11:25 PM

David said:

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

TransactionScope was in 2.0 :D

# April 29, 2008 3:08 PM

Venkat said:

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

# September 11, 2008 1:45 PM

mehmet murat huyut said:

Thanks

an answer to the equivalent in VB :same :)

try it not so different..

# December 15, 2008 2:16 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker