共用方式為


Table<TEntity>.DeleteOnSubmit(TEntity) 方法

定義

將此資料表中的實體置於 pending delete 狀態。

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)

參數

entity
TEntity

要刪除的實體。

實作

範例

// 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

備註

在呼叫 之後, SubmitChanges 移除的實體不會從查詢結果中消失。 必須先附加中斷連線的實體,才能刪除它們。 如需詳細資訊,請參閱多層式架構應用程式中的資料擷取和 CUD 作業 (LINQ to SQL)

注意

LINQ to SQL 不支援或辨識串聯刪除作業。 如果您要刪除有限制式之資料表中的資料列,必須在資料庫的外部索引鍵限制式中設定 ON DELETE CASCADE 規則,或者使用您自己的程式碼,先刪除使父物件無法刪除的子物件。 否則,會擲回例外狀況。

適用於