首页 > 解决方案 > ef core plus:ArgumentNullException:值不能为空。参数名称:属性

问题描述

使用 ef core plus,我正在尝试更新模型上的一组行:

await _context.Set<Critique>()
            .Include(c => c.User)
            .Where(c => c.Closed && c.User.Id == user.Id)
            .UpdateAsync(c => new Critique() { User = deletedUser });

在这里,我得到以下异常:

ArgumentNullException:值不能为空。参数名称:Check.cs 第 21 行中的属性 Npgsql.EntityFrameworkCore.PostgreSQL.Utilities.Check.NotNull(T value, string parameterName)

TargetInvocationException:调用的目标已引发异常。System.RuntimeMethodHandle.InvokeMethod(object target, object[] arguments, Signature sig, bool constructor, bool wrapException

当我只用 ef core 加载数据时,它会加载我期望的 1 行

var test = await _context.Set<Critique>()
            .Where(c => c.Closed && c.User.Id == user.Id)
            .ToArrayAsync();

数据库模型没什么花哨的,但是太大了,无法在此处发布。我正在广泛使用 [Required],并且有很多一对多关系。

(顺便说一句,user.Id 不为 null 且可用,并且 deletedUser 在代码和数据库中也可用。)

应该如何解释这个错误?我究竟做错了什么?

谢谢你的帮助!

标签: entity-framework-coreentity-framework-plus

解决方案


我们的库现在可以更新值但不能使用导航属性更新值

这不起作用

.UpdateAsync(c => new Critique() { User = deletedUser });

但是,如果您拥有该财产,这将起作用

.UpdateAsync(c => new Critique() { UserID = deletedUserID });

.Include(c => c.User)此外,由于您不检索此信息,因此没有必要添加。


推荐阅读