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
{
开发者_JAVA百科 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
declare @when datetime = GETUTCDATE() - 1
delete from ShoppingCarts where DateCreated < @when
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.
Somewhat obvious question but could be your problem: are you certain your dates in the database are in UTC format?
Other things you might like to try are sharing the code in your "try" statement (I assume something is actually happening in there and you've just left it out) and also using SQL Profiler to take a look at what's executing on the SQL Server, both in the original select statement and subsequent deletations.
Entity framework right?
db.DeleteObject(item);
db.SaveChanges();
http://msdn.microsoft.com/en-us/library/bb386870.aspx
You need to select an actual element, it's not exactly clear what you are trying to delete.
精彩评论