Editare

Partajați prin


Table<TEntity>.DeleteOnSubmit(TEntity) Method

Definition

Puts an entity from this table into a pending delete state.

public:
 void DeleteOnSubmit(TEntity entity);
public:
 virtual void DeleteOnSubmit(TEntity entity);
public void DeleteOnSubmit (TEntity entity);
member this.DeleteOnSubmit : 'Entity -> unit
abstract member DeleteOnSubmit : 'Entity -> unit
override this.DeleteOnSubmit : 'Entity -> unit
Public Sub DeleteOnSubmit (entity As TEntity)

Parameters

entity
TEntity

The entity to be deleted.

Implements

Examples

// Query the database for the rows to be deleted.
var deleteOrderDetails =
    from details in db.OrderDetails
    where details.OrderID == 11000
    select details;

foreach (var detail in deleteOrderDetails)
{
    db.OrderDetails.DeleteOnSubmit(detail);
}

try
{
    db.SubmitChanges();
}
catch (Exception e)
{
    Console.WriteLine(e);
    // Provide for exceptions.
}
' Query the database for the rows to be deleted.
Dim deleteOrderDetails = _
    From details In db.OrderDetails() _
    Where details.OrderID = 11000 _
    Select details

For Each detail As OrderDetail In deleteOrderDetails
    db.OrderDetails.DeleteOnSubmit(detail)
Next

Try
    db.SubmitChanges()
Catch ex As Exception
    Console.WriteLine(ex)
    ' Provide for exceptions
End Try

Remarks

The removed entity does not disappear from the query results until after SubmitChanges is called. Disconnected entities must first be attached before they can be deleted. For more information, see Data Retrieval and CUD Operations in N-Tier Applications (LINQ to SQL).

Note

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.

Applies to