简体   繁体   English

实体框架加上日期时间批量删除不起作用

[英]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.似乎 EF+ 批量删除 function 没有正确地等同日期时间并执行操作。

It does work fine on other field types, but just not DateTime.它确实适用于其他字段类型,但不是 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) .后端 SQL 服务器表的FileCreatedDateDateTime2(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.并且记录都包含代码中指定的DateTime.UtcNow日期/时间,因为在此代码之前的初始传递创建了这些记录。 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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