Inserting with LINQ to SQL

There are two steps to adding and inserting a new record with LINQ to SQL. First, you need to use the InsertOnSubmit() method to add an entity to an existing table. Next, you call SubmitChanges() on the DataContext to execute the SQL INSERT statement against the database.

The class in Listing 20.22 illustrates how you can write a method to add a new record into the Movie database table.

Listing 20.22. Standard\App_Code\Movie.cs

images

The Movie class includes an Insert() method that inserts a new movie into the database. The Insert() method returns an integer that represents the identity value of the new record. As soon as ...

Get ASP.NET 4 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.