实体框架删除一些记录
本文关键字:记录 删除 框架 实体 | 更新日期: 2023-09-27 17:54:11
实体框架删除一些记录
using (Galway__Entities db = new Galway__Entities())
{
DateTime RemovableDate = new DateTime();
List<PROJ_ACCS_StockControl_DeletedPLURecord> DeletedPLURecords = db.PROJ_ACCS_StockControl_DeletedPLURecord.Where(x => x.TimeStamp <= RemovableDate).ToList();
db.??????
//so I delete the list all at once or use a foreach and run round each record?
//how do I delete each singular record if this is the case then.
}
您可以使用:
DeletedPLURecords.ForEach(element => db.PROJ_ACCS_StockControl_DeletedPLURecord.Remove(element));
db.SaveChanges();
试试这个:
using (Galway__Entities db = new Galway__Entities())
{
DateTime RemovableDate = new DateTime();
List<PROJ_ACCS_StockControl_DeletedPLURecord> DeletedPLURecords = db.PROJ_ACCS_StockControl_DeletedPLURecord.Where(x => x.TimeStamp <= RemovableDate).ToList();
foreach (var item in DeletedPLURecords)
{
var e = db.PROJ_ACCS_StockControl_DeletedPLURecord.Find(item.Id);
if (e != null)
{
Db.PROJ_ACCS_StockControl_DeletedPLURecord.Remove(e);
}
}
db.SaveChanges();
}
当前要删除多个记录,您需要首先在内存中获取记录,然后删除它们。另一个解决方案是你可以使用EntityFramework。扩展库使其成为单个请求过程。像
....
db.PROJ_ACCS_StockControl_DeletedPLURecord.Where(x => x.TimeStamp <= RemovableDate).Delete();