首页 新闻 会员 周边

EF Core 7.0 遇到 "The LINQ expression could not be translated" 问题

0
悬赏园豆:50 [待解决问题]

EF Core 7.0 中使用下面的 LINQ :

await DbSet.ById(userId).ExecuteUpdateAsync(u =>
    u.SetProperty(x => x.LastLogin, x => DateTime.Now));

在运行时报错:

Error Message:
System.InvalidOperationException : The LINQ expression 'DbSet<User>()
.Where(u => ((IEntity<Guid>)u).Id.Equals((object)__id_0))
.ExecuteUpdate(u => u.SetProperty<DateTime?>(
    propertyExpression: x => x.LastLogin, 
    valueExpression: x => (DateTime?)DateTime.Now))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
Stack Trace:
    at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)

请问如何解决?

dudu的主页 dudu | 高人七级 | 园豆:31007
提问于:2022-12-16 12:34
< >
分享
所有回答(2)
0

改成类似这样呢

await DbSet.Where(u => u.Id == userId)
    .SetProperty(u => u.LastLogin, DateTime.Now)
    .UpdateAsync();
宝树呐 | 园豆:318 (菜鸟二级) | 2022-12-16 15:52

SetProperty 不支持 IQueryable,ExecuteUpdateAsync 是 EF Core 7 引入的新特性,详见 ExecuteUpdate and ExecuteDelete (Bulk updates)

支持(0) 反对(0) dudu | 园豆:31007 (高人七级) | 2022-12-16 16:13
0

是 EF Core In-Memory Database 的问题,用 SQL Server 时没这个问题

dudu | 园豆:31007 (高人七级) | 2022-12-16 16:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册