Entity Framework composite key delete with one primary key

c# entity-framework-6 repository-pattern

Question

Here's some of my database design on my assignments

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

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

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

I'd like to make a function clearUserCart(User user) in my Cart Repository class but I have no idea how to make function to delete all rows with specified user.

EDIT : I have figured out how to do it, just use getCartOfUser(User user) which returns List<Cart>. Then do foreach loop to remove. In SQL, it could be achieved just by one line DELETE FROM Cart WHERE CustomerID = <the cust id>, is there an equivalent one line statement in entity framework?

1
0
4/12/2017 12:48:19 PM

Popular Answer

If you are using EF 6, you can use the RemoveRange method.

So it could look something like this;

context.Cart.RemoveRange(cartItem => cartItem.CustomerID == CustomerToDelete);
1
4/12/2017 12:57:39 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow