Linq Insert Sample Code

If you want to add a record to a table or remove a record from a table, creating or deleting an object in memory is not enough. The DataContext instance must be notified also. This can be
done directly by calling Add or Remove on the corresponding Table collection (these methods
operate on the in-memory copy of the data; a subsequent SubmitChanges call will forward the
SQL commands to the database):

var newCustomer = new Customer {
CustomerID = “DLEAP”,
CompanyName = “DevLeap”,
Country = “Italy” };
db.Customers.Add( newCustomer );
var oldCustomer = db.Customers.Single( c => c.CustomerID == “FRANS” );
db.Customers.Remove( oldCustomer );

3 Comments»

  1. Where did you get your blog layout from? I’d like to get one like it for my blog.

    Comment by Dan Waldron — June 28, 2008@ 5:16 pm

  2. Or, use

    db.Customers.DeleteOnSubmit(oldCustomer)

    Comment by Paladin — July 1, 2008@ 5:37 pm

  3. Your blog is interesting!

    Keep up the good work!

    Comment by Alex — August 17, 2008@ 8:02 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Spam protection by WP Captcha-Free

Powered by Mucitsoft