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 已删除的实体不会从查询结果中消失。 必须先附加断开连接的实体,然后才能将其删除。 有关详细信息,请参阅 N 层应用程序中的数据检索和 CUD 操作 (LINQ to SQL)

备注

LINQ to SQL 不支持或无法识别级联删除操作。 如果要从表中删除一个具有约束的行,必须在数据库的外键约束中设置 ON DELETE CASCADE 规则,或者使用自己的代码首先删除那些阻止删除父对象的子对象。 否则会引发异常。

适用于