I was looking at other examples but I have not opened anything, where would I close the information at. It happenes when I go to _DBContext.SaveChanges(); Additionally, the code says if the Wedding is less then DateTimeNow to do this action, however, it does it for all dates. I have read throuhg many questions and answers they are all pretty different to me as a newbie trying to make a method in the controller. Should I be making this method in the conroller or somewhere else?
// Delete old weddings from comparing date times
public void DeleteOldWeddings()
{
foreach (Wedding date in _DBContext.Weddings)
{
var result = DateTime.Compare(date.WedDate, DateTime.Now);
if (result < 0)
{
Console.WriteLine("This " date.UserID);
_DBContext.Weddings.Remove(date);
_DBContext.SaveChanges();
}
}
}
CodePudding user response:
The enumerator for your foreach loop is what’s open. Call .ToArray() or similar to finish reading the query results before attempting another operation with the DbContext.
foreach(var date in _DBContext.Weddings.ToArray())
{
...
}
