| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | ms.assetid | caps.latest.revision | author | ms.author | manager | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Insert, Update, and Delete Operations |
03/30/2017 |
.net-framework |
|
article |
26a43a4f-83c9-4732-806d-bb23aad0ff6b |
3 |
JennieHubbard |
jhubbard |
jhubbard |
Insert, Update, and Delete Operations
You perform Insert, Update, and Delete operations in [!INCLUDEvbtecdlinq] by adding, changing, and removing objects in your object model. By default, [!INCLUDEvbtecdlinq] translates your actions to SQL and submits the changes to the database.
[!INCLUDEvbtecdlinq] 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. [!INCLUDEvbtecdlinq] tracks your changes and is ready to transmit them back to the database when you call xref:System.Data.Linq.DataContext.SubmitChanges%2A.
[!NOTE] [!INCLUDEvbtecdlinq] 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 CASCADErule 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.
The following excerpts use the Customer and Order classes from the Northwind sample database. Class definitions are not shown for brevity.
[!code-csharpDLinqCRUDOps#1] [!code-vbDLinqCRUDOps#1]
When you call xref:System.Data.Linq.DataContext.SubmitChanges%2A, [!INCLUDEvbtecdlinq] automatically generates and executes the SQL commands that it must have to transmit your changes back to the database.
[!NOTE] 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.
Developers using [!INCLUDEvs_current_short] can use the [!INCLUDEvs_ordesigner_long] to develop stored procedures for this purpose.
See Also
Downloading Sample Databases
Customizing Insert, Update, and Delete Operations