ObjectContext.DeleteObject(Object) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將物件標記為待刪除。
public:
void DeleteObject(System::Object ^ entity);
public void DeleteObject (object entity);
member this.DeleteObject : obj -> unit
Public Sub DeleteObject (entity As Object)
參數
例外狀況
entity
為 null
。
entity
不存在。
範例
此範例會 EntityKey 建構具有特定 ProductID 的、使用索引鍵從數據源擷取 Product 物件、刪除產品,以及將變更儲存至資料庫。
object deletedProduct;
// Define the key of the product to delete.
EntityKey productKey =
new EntityKey("AdventureWorksEntities.Products",
"ProductID", productId);
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
// Get the object to delete with the specified key.
if (context.TryGetObjectByKey(productKey, out deletedProduct))
{
try
{
// Delete the object with the specified key
// and save changes to delete the row from the data source.
context.DeleteObject(deletedProduct);
context.SaveChanges();
}
catch (OptimisticConcurrencyException ex)
{
throw new InvalidOperationException(string.Format(
"The product with an ID of '{0}' could not be deleted.\n"
+ "Make sure that any related objects are already deleted.\n",
productKey.EntityKeyValues[0].Value), ex);
}
}
else
{
throw new InvalidOperationException(string.Format(
"The product with an ID of '{0}' could not be found.\n"
+ "Make sure that Product exists.\n",
productKey.EntityKeyValues[0].Value));
}
}
本範例會刪除現有的訂單專案、新增專案,並將變更儲存至資料庫。
// Specify the order to update.
int orderId = 43680;
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
try
{
var order = (from o in context.SalesOrderHeaders
where o.SalesOrderID == orderId
select o).First();
// Change the status and ship date of an existing order.
order.Status = 1;
order.ShipDate = DateTime.Today;
// You do not have to call the Load method to load the details for the order,
// because lazy loading is set to true
// by the constructor of the AdventureWorksEntities object.
// With lazy loading set to true the related objects are loaded when
// you access the navigation property. In this case SalesOrderDetails.
// Delete the first item in the order.
context.DeleteObject(order.SalesOrderDetails.First());
// Create a new SalesOrderDetail object.
// You can use the static CreateObjectName method (the Entity Framework
// adds this method to the generated entity types) instead of the new operator:
// SalesOrderDetail.CreateSalesOrderDetail(1, 0, 2, 750, 1, (decimal)2171.2942, 0, 0,
// Guid.NewGuid(), DateTime.Today));
SalesOrderDetail detail = new SalesOrderDetail
{
SalesOrderID = 1,
SalesOrderDetailID = 0,
OrderQty = 2,
ProductID = 750,
SpecialOfferID = 1,
UnitPrice = (decimal)2171.2942,
UnitPriceDiscount = 0,
LineTotal = 0,
rowguid = Guid.NewGuid(),
ModifiedDate = DateTime.Now
};
order.SalesOrderDetails.Add(detail);
// Save changes in the object context to the database.
int changes = context.SaveChanges();
Console.WriteLine(changes.ToString() + " changes saved!");
Console.WriteLine("Updated item for order: "
+ order.SalesOrderID.ToString());
foreach (SalesOrderDetail item in order.SalesOrderDetails)
{
Console.WriteLine("Item ID: "
+ item.SalesOrderDetailID.ToString() + " Product: "
+ item.ProductID.ToString() + " Quantity: "
+ item.OrderQty.ToString());
}
}
catch (UpdateException ex)
{
Console.WriteLine(ex.ToString());
}
}
備註
從 ObjectStateManager 將物件標記為待刪除。 呼叫 SaveChanges 方法時,就會在資料來源中刪除此物件。
刪除父物件也會刪除條件約束關聯性中的所有子物件。 這個結果與在關聯性的關聯上啟用 CascadeDelete
屬性相同。
DeleteObject方法可以在已刪除的物件上呼叫。