gpt4 book ai didi

asp.net - Entity Framework Core - 这可能表示 EF Core 中的错误或限制

转载 作者:行者123 更新时间:2023-12-04 10:29:29 25 4
gpt4 key购买 nike

我想从用户那里加载最新的私有(private)聊天消息。

我加载最新消息的代码

 public async Task<List<UserChatMessage>> GetUserPrivateChatMessagesAsync(string userId, string userChatPartnerId, int limit, int skip, CancellationToken cancellationToken) {
cancellationToken.ThrowIfCancellationRequested();
if (userId == null) throw new ArgumentNullException(nameof(userId));
if (userChatPartnerId == null) throw new ArgumentNullException(nameof(userChatPartnerId));

return await this.Context.Messages
.OrderBy(d => d.CreatedDate)
.AsNoTracking()
.Include(p => p.UserChatPartner)
.Where(u => u.UserId == userId && u.UserChatPartnerId == userChatPartnerId || u.UserChatPartnerId == userId && u.UserId == userChatPartnerId)
.TakeLast(limit)
.Skip(skip)
.ToListAsync(cancellationToken);
}


以下代码抛出此错误:
System.InvalidOperationException: Processing of the LINQ expression 'DbSet<UserChatMessage>
.OrderBy(d => d.CreatedDate)
.Include(p => p.UserChatPartner)
.Where(u => u.UserId == __userId_0 && u.UserChatPartnerId == __userChatPartnerId_1 || u.UserChatPartnerId == __userId_0 && u.UserId == __userChatPartnerId_1)
.TakeLast(__p_2)' by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core. See https://go.microsoft.com/fwlink/?linkid=2101433 for more detailed information.

我将 .Take 更改为 .TakeLast ,现在我得到了描述的错误。

UserChatMessage的数据库结构
Database strcuture of UserChatMessage

我还尝试将 ToListAsync 更改为 AsEnumerable ,但没​​有成功。

我希望有人理解错误并可以帮助我。

提前致谢。
关于蒂莫

最佳答案

该错误意味着您遇到了当前的 EF Core 错误或限制。

I changed .Take to .TakeLast and now I get the described error.



所以你知道是什么导致了这个问题。通常避免使用具有 Last 的 LINQ 方法。名义上(如 LastLastOrDefaultTakeLast ) - 这些在 SQL 世界中没有直接等效项,因此查询翻译器更有可能遇到错误/限制(或不支持)。

相反,颠倒排序并使用相应的 First方法。

将其应用于您的案例意味着更换
.OrderBy(d => d.CreatedDate)


.OrderByDescending(d => d.CreatedDate)


.TakeLast(limit)


.Take(limit)

关于asp.net - Entity Framework Core - 这可能表示 EF Core 中的错误或限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60474045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com