EF核心包括在数据之间

发布于 2025-02-13 04:43:37 字数 728 浏览 1 评论 0原文

我正在将ASP.NET Core与实体框架Core一起使用,并且正在尝试在特定的DateTime框架内过滤结果。

这是我的dbContext查询:

var device = await _context.Devices.Where(d => d.Id == id)
            .Include(d => d.Layouts.Where(i => i.CreatedAt > fromDateTime && i.CreatedAt < toDateTime))
            .ThenInclude(a => a.States)
            .FirstOrDefaultAsync();

'CreateAt'在SQL Server中的“ DateTime”格式中,“ FromDateTime”和“ TodateTime”也是DateTime类型。

直接查询基本上是即时的:

SELECT * [Id]
        ,[CreatedAt]
 FROM [Layout]
 Where CreatedAt > '2022-07-03 00:00:00'
 And CreatedAt < '2022-07-04 00:00:00'

但是从后端开始,我花了很长时间,我没有等到完成,而是20分钟。

我需要帮助才能找到我在做错什么,或者如何编写查询以使其更快。 谢谢。

I am using ASP.NET CORE with Entity Framework Core and I am trying to filter out result within a specific DATETIME frame.

This is my DBContext query:

var device = await _context.Devices.Where(d => d.Id == id)
            .Include(d => d.Layouts.Where(i => i.CreatedAt > fromDateTime && i.CreatedAt < toDateTime))
            .ThenInclude(a => a.States)
            .FirstOrDefaultAsync();

'CreatedAt' is in 'datetime' format in the SQL Server, 'fromDateTime' and 'toDateTime' is also DateTime type.

The direct query is basically instant:

SELECT * [Id]
        ,[CreatedAt]
 FROM [Layout]
 Where CreatedAt > '2022-07-03 00:00:00'
 And CreatedAt < '2022-07-04 00:00:00'

But from the backend it takes very long, I did not wait until if finishes but more 20 minutes.

I would need help to find what I am doing wrong or how can I write the query to be faster.
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看轻我的陪伴 2025-02-20 04:43:37

添加assplitquery(),您渴望加载记录的笛卡尔爆炸。另外,您的SQL不是相同的查询,急切的加载没有直接转换为SQL。 -
Svyatoslav Danyliv
7月4日在9:11

Add AsSplitQuery(), you have Eager Loading with Cartesian Explosion of records. Also you SQL is not the same query, Eager Loading has no direct translation to the SQL. –
Svyatoslav Danyliv
Jul 4 at 9:11

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文