简体   繁体   中英

Entity Framework Plus Batch Delete on DateTime Not working

I have run into a scenario where I need to bulk delete on a specific date time. It seems that the EF+ batch delete function doesn't correctly equate date time and perform the action.

It does work fine on other field types, but just not DateTime. Is there some special formatting I should be doing to perform this action properly?

The backend SQL Server table has the FileCreatedDate as DateTime2(7) . And records all contain the DateTime.UtcNow date/time that is specified in the code, since an initial pass prior to this code created those records. This is a fallback to clean out the records that got created if an error occurred.

I have the following code:

var fileCreatedDate = DateTime.UtcNow;

using (var db = new MyContextDb())
{
    db.SampleModel.Where(e => e.FileCreatedDate == fileCreatedDate).Delete();
}

This code doesn't work. It doesn't delete the records whose time matches the criteria.

Any ideas what might be the problem?

Can you try to remove the entities using range?

like so:

db.SampleModel.RemoveRange(db.SampleModel.Where(e => e.FileCreatedDate == fileCreatedDate));
db.SaveChanges();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM