[英]Entity Framework Count Async Throws Exception
We had a synchrounous call to get a count of disposals that meet certain criteria. 我们进行了同步通话,以获取满足特定条件的处置数量。 The working query looks like this.
工作查询如下所示。
itemsCount = _db.Disposals
.Include(d => d.ItemIds)
.AsEnumerable()
.Where(d => d.OrganizationId == SelectedOrganizationID &&
d.CreateDateTime.UtcDateTime > greaterThen.ToUniversalTime() &&
d.CreateDateTime.UtcDateTime < lessThen.ToUniversalTime())
.SelectMany(d => d.ItemIds)
.Count();
I needed to make the query async. 我需要使查询异步。 It seems that I need an IQueryable to run CountAsync on it.
看来我需要一个IQueryable才能在其上运行CountAsync 。 I don't understand why the code above had
.AsEnumerable()
in the first place, and why the code below throws an exception: 我不明白上面的代码为什么首先具有
.AsEnumerable()
,以及为什么下面的代码引发异常:
A first chance exception of type 'System.NotSupportedException' occurred in mscorlib.dll
mscorlib.dll中发生类型'System.NotSupportedException'的第一次机会异常
itemsCount = await _db.Disposals
.Include(d => d.ItemIds)
.Where(d => d.OrganizationId == SelectedOrganizationID &&
d.CreateDateTime.UtcDateTime > greaterThen.ToUniversalTime() &&
d.CreateDateTime.UtcDateTime < lessThen.ToUniversalTime())
.SelectMany(d => d.ItemIds)
.CountAsync();
I am really just looking for why the async count doesn't work (the project does build, but then the exception is thrown). 我真的只是在寻找为什么异步计数不起作用的原因(项目确实可以构建,但是会引发异常)。 However, it would be a bonus to understand why
AsEnumerable()
is used. 但是,了解为什么使用
AsEnumerable()
将是一个好处。
PS I am aware that it's not running asynchrounously when I put await in front of it, that's just there for testing. PS我知道,当我在它前面等待时,它并没有异常运行,仅用于测试。
_db
is the EF database context. _db
是EF数据库上下文。
edit: It still throws an exception when written as: 编辑:编写为时仍会引发异常:
var greaterThanUtc = greaterThen.ToUniversalTime();
var lessThanUtc = greaterThen.ToUniversalTime();
itemsCount = await _db.Disposals
.Include(d => d.ItemIds)
.Where(d => d.OrganizationId == SelectedOrganizationID &&
d.CreateDateTime.UtcDateTime > greaterThanUtc &&
d.CreateDateTime.UtcDateTime < lessThanUtc)
.SelectMany(d => d.ItemIds)
.CountAsync();
A first chance exception of type 'System.NotSupportedException' occurred in mscorlib.dll
mscorlib.dll中发生类型'System.NotSupportedException'的第一次机会异常
EDIT 2: 编辑2:
I think the problem is trying convert a Date field in SQL (datetimeoffset) to d.CreateDateTime.UtcDateTime, I am guessing EF doesn't support this at all. 我认为问题是尝试将SQL(datetimeoffset)中的Date字段转换为d.CreateDateTime.UtcDateTime,我想EF根本不支持此功能。
The function call ToUniversalTime()
cannot be translated to a store expression, so it cannot run on the database side. 函数调用
ToUniversalTime()
无法转换为存储表达式,因此不能在数据库端运行。
.AsEnumerable()
causes everything that comes after it to run on the client, so you can use that .NET method. .AsEnumerable()
使它之后的所有内容都在客户端上运行,因此您可以使用该.NET方法。
Note it's generally a bad idea to run more client-side than necessary. 请注意,运行过多的客户端通常不是一个好主意。 The calls to
ToUniversalTime()
happen on what appear to be local variables. ToUniversalTime()
的调用发生在看起来是局部变量的地方。 You could so something like 你可以这样
var greaterThanUtc = greaterThen.ToUniversalTime()
and then use greaterThanUtc in your query, with out .ToUniversalTime(); 然后在查询中使用而不使用.ToUniversalTime();
Same for lessThen
. lessThen
相同。
UPDATE UPDATE
If your database field is datetimeoffset
you have to use DateTimeOffset in C#. 如果您的数据库字段是
datetimeoffset
,则必须在C#中使用DateTimeOffset。 See Entity Framework Mapping DateTimeOffset to SQL Server DateTime 请参见实体框架将DateTimeOffset映射到SQL Server DateTime
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.