2009-10-21 35 views
3

我知道我可以將刪除存儲過程映射到特定類型的刪除方法。如何從我的表中刪除一行或多行使用Linq to Entities *而不先*檢索行?

但是,這需要將檢索到的對象傳遞給我的上下文的DeleteObject方法。

這已經夠糟糕了,但是如果我想刪除2000行呢?我可以使用Linq to Entities做到這一點,而無需先從數據庫中檢索這些2000行,然後通過調用DeleteObject的循環?

如果這樣的功能並不Linq中存在的實體,你知道這是,那麼請你只說這樣的情況下,我會調查其他的選擇!

如果它不直接存在,我可以通過將存儲過程通過Linq傳遞給實體來實現嗎?

回答

3

是的,你可以做到這一點。從this tip

// Create an entity to represent the Entity you wish to delete 
// Notice you don't need to know all the properties, in this 
// case just the ID will do. 
Category stub = new Category { ID = 4 }; 
// Now attach the category stub object to the "Categories" set. 
// This puts the Entity into the context in the unchanged state, 
// This is same state it would have had if you made the query 
ctx.AttachTo("Categories", stub); 
// Do the delete the category 
ctx.DeleteObject(stub); 
// Apply the delete to the database 
ctx.SaveChanges(); 

the full tip詳情及併發症。

+0

Ahhhh ..這個想法簡單地進入了我的頭,但我認爲它太瘋狂了,所以我認爲「更好地諮詢SO」!謝謝。 AttachTo也是一個問題。 – joshcomley 2009-10-21 15:42:13

相關問題