I am trying to remove items that are created more than 1 day old, however all my items still remain. I am certain that the changes are committed to my database since I am using the RemoveItem method in other locations as well.
using (var db = new CommerceEntities())
{
DateTime checkexpiryDate = DateTime.UtcNow.AddDays(-1);
var itemstoDelete = from c in db.ShoppingCarts
where c.DateCreated < checkexpiryDate
select new
{
c.CartID,
c.ProductID,
c.Color,
c.Size,
};
foreach (var item in itemstoDelete)
{
try
{
//remove item code....
//RemoveItem(string cartID, int productID, string color, string size)
}
public void RemoveItem(string cartID, int productID, string color, string size)
{
using (var db = new CommerceEntities())
{
....
..
//db.DeleteObject(myItem);
....
..
//db.SaveChanges();
...
....
..
}
}
IMO, the preferred way to do that would be TSQL, not an ORM
which is trivial to execute via either ADO.NET or any helper utility.
that avoids dragging an unknown amount of data over the wire, and avoids a lengthy transaction while you perform multiple discreet operations.