LINQ to SQL Insert

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

In LINQ to SQL, you can easily insert data into a database by adding objects to Table<TEntity> collection and then call SubmitChanges() method. LINQ to SQL will translate your changes into the appropriate SQL INSERT commands.

The following code creates a customer object and inserts it into the Customers table.


using (var db = new CustomerDataContext(@"Data Source=(localdb)\ProjectsV13;Initial Catalog=CustomerDB;"))
{
    var customer = new Customer()
    {
        FirstName = "Maria",
        LastName = "Anders",
        Address = "Obere Str. 57"
    };
    
    db.Customers.InsertOnSubmit(customer);
    
    db.SubmitChanges();
}



Got any LINQ to SQL Question?