2017-04-12 102 views
0

下面是一些我的數據庫設計對我的作業實體框架複合鍵刪除一個主鍵

Customer 
-------- 
CustomerID (PK) 

Cake 
------- 
CakeID (PK) 

Cart 
------- 
CustomerID (PK, FK) 
CakeID (PK, FK) 

我想提出一個功能clearUserCart(User user)在我的車庫類,但我不知道怎麼做函數刪除指定用戶的所有行。

編輯: 我已經找到了如何做到這一點,只需使用getCartOfUser(User user)返回List<Cart>。然後做foreach循環來刪除。 在SQL中,它可以實現只有一行DELETE FROM Cart WHERE CustomerID = <the cust id>,在實體框架中是否存在等價的一行語句?

+0

我想讀[這](https://support.microsoft.com/en-us/help/2802240/crud-using-entity-framework-in-.net-framework-5.0---刪除)將幫助你 –

回答

0

1)您可以使用context.Carts.RemoveRange(queryable.Where(c => c.CustomerID == <the cust id>)).
2)您可以添加參考Entity Framework Extended Library,如果你使用的是EF 6使用這樣的語法context.Carts.Where(c => c.CustomerID == <the cust id>).Delete()

1

,您可以使用是,removeRange方法。

因此,它可能看起來像這樣;

context.Cart.RemoveRange(cartItem => cartItem.CustomerID == CustomerToDelete);