Tuesday, May 25, 2010

Insert, Update, and Delete Operations (LINQ to SQL)

Insert, Update, and Delete Operations (LINQ to SQL): "HomeLibraryLearnDownloadsSupportCommunity

Sign in |United States - English |Preferences

MSDN Library
.NET Development
.NET Framework 4
.NET Framework Core Development
Data Access and Modeling
ADO.NET
SQL Server and ADO.NET
LINQ to SQL
Programming Guide (LINQ to SQL)
Background Information (LINQ to SQL)
Customizing Insert, Update, and Delete Operations (LINQ to SQL)
Insert, Update, and Delete Operations (LINQ to SQL)
Separator
MSDN
LINQ to SQL
Insert, Update, and Delete Operations (LINQ to SQL)

You perform Insert, Update, and Delete operations in LINQ to SQL by adding, changing, and removing objects in your object model. By default, LINQ to SQL translates your actions to SQL and submits the changes to the database.

LINQ to SQL offers maximum flexibility in manipulating and persisting changes that you made to your objects. As soon as entity objects are available (either by retrieving them through a query or by constructing them anew), you can change them as typical objects in your application. That is, you can change their values, you can add them to your collections, and you can remove them from your collections. LINQ to SQL tracks your changes and is ready to transmit them back to the database when you call SubmitChanges.
NoteNote

LINQ to SQL does not support or recognize cascade-delete operations. If you want to delete a row in a table that has constraints against it, you must either set the ON DELETE CASCADE rule in the foreign-key constraint in the database, or use your own code to first delete the child objects that prevent the parent object from being deleted. Otherwise, an exception is thrown. For more information, see How to: Delete Rows From the Database (LINQ to SQL).

The following excerpts use the Customer and Order classes from the Northwind sample database. Class definitions are not shown for brevity.
VB
C#
C++
F#
JScript
Copy


Northwnd db = new Northwnd(@'c:\Northwnd.mdf');

// Query for a specific customer.
var cust =
(from c in db.Customers
where c.CustomerID == 'ALFKI'
select c).First();

// Change the name of the contact.
cust.ContactName = 'New Contact';

// Create and add a new Order to the Orders collection.
Order ord = new Order { OrderDate = DateTime.Now };
cust.Orders.Add(ord);

// Delete an existing Order.
Order ord0 = cust.Orders[0];

// Removing it from the table also removes it from the Customer’s list.
db.Orders.DeleteOnSubmit(ord0);

// Ask the DataContext to save all the changes.
db.SubmitChanges();


When you call SubmitChanges, LINQ to SQL automatically generates and executes the SQL commands that it must have to transmit your changes back to the database.
NoteNote

You can override this behavior by using your own custom logic, typically by way of a stored procedure. For more information, see Responsibilities of the Developer In Overriding Default Behavior (LINQ to SQL).

Developers using Visual Studio can use the Object Relational Designer to develop stored procedures for this purpose. For more information, see Object Relational Designer (O/R Designer) and Object Relational Designer (O/R Designer).
See Also
Concepts
Downloading Sample Databases (LINQ to SQL)
Other Resources
Customizing Insert, Update, and Delete Operations (LINQ to SQL)
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Feedback
Feedback on the Lightweight view
x
Lightweight builds on ScriptFree (loband) by adding features users have requested: a search box and default code language selection
Did the page load quickly?
Yes No
Do you like the page design?
Yes No
How useful is this topic?
Tell us more
Page view tracker


- Sent using Google Toolbar"

No comments:

Post a Comment