UpdateSetColumnsTrueAsync适配异常 返回
在.net10环境下,sqlsuagr版本5.1.4.211,使用如下代码,会报错,主要原因是前端传入可空类型,不加!符号,编辑器报绿色提示,可能为null
await repositorySystemUser.UpdateSetColumnsTrueAsync(s => new()
{
Name = input.Name,
IdCard = input.IdCard,
SystemOrgId = input.SystemOrgId!.Value,
Num = input.Num,
Tel = input.Tel,
IsUsed = input.IsUsed!.Value
}, s => s.Id == input.Id);
需要改成如下代码
var SystemOrgId = input.SystemOrgId!.Value;
var IsUsed = input.IsUsed!.Value;
await repositorySystemUser.UpdateSetColumnsTrueAsync(s => new()
{
Name = input.Name,
IdCard = input.IdCard,
SystemOrgId = SystemOrgId,
Num = input.Num,
Tel = input.Tel,
IsUsed = IsUsed
}, s => s.Id == input.Id);
热忱回答(8)
-
fate sta VIP0
2026/3/5这个和ORM没有关系。是你编译器的限制。
0 回复 -
慢慢来、不着 VIP0
2026/3/5@fate sta:加上感叹哈,sqlsuagr会报错,输出SQL如下:
[Sql]:UPDATE `SystemUser` SET
`Name` = @Const0 , `IdCard` = @Const1 , `SystemOrgId` = @Const2 , `Num` = @Const3 , `Tel` = @Const4 , `IsUsed` = ( value(HeartOrange.Main.Controllers.Systems.Users.SystemUserController+<>c__DisplayClass8_0).input.IsUsed=1 ) ,`UTime`=NOW(6) WHERE ( `Id` = @Id5 )
[Pars]:
[Name]:@Const0 [Value]:测试一号 [Type]:String
[Name]:@Const1 [Value]:500232199005121234 [Type]:String
[Name]:@Const2 [Value]:1 [Type]:Int64
[Name]:@Const3 [Value]: [Type]:AnsiString
[Name]:@Const4 [Value]: [Type]:AnsiString
[Name]:@Id5 [Value]:779285649839877 [Type]:Int64
[Name]:@Id [Value]:0 [Type]:Int64
IsUsed是bool?类型,SystemOrgId是long?类型。我测试了一下,long?不会报错,另一个接口有枚举enmu?类型,也不会报错。只有bool?类型转换时报错了
错误输出如下:MySqlConnector.MySqlException (0x80004005): You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 2 column 142 near "(HeartOrange.Main.Controllers.Systems.Users.SystemUserController+<>c__DisplayClass8_0).input.IsUsed=1 ) ,`UTime`=NOW(6) WHERE ( `Id` = 779285649839877 )"
at SqlSugar.MySqlProvider.ExecuteCommandAsync(String sql, SugarParameter[] parameters)
at SqlSugar.MySqlProvider.ExecuteCommandAsync(String sql, SugarParameter[] parameters)
at SqlSugar.UpdateableProvider`1.ExecuteCommandAsync()
at SqlSugar.SimpleClient`1.UpdateSetColumnsTrueAsync(Expression`1 columns, Expression`1 whereExpression)
at HeartOrange.Main.Controllers.Systems.Users.SystemUserController.UserEdit(UserEditInput input) in E:\Project\HeartOrange\HeartOrange.Main\Controllers\Systems\Users\SystemUserController.cs:line 129
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at HeartOrange.Main.Middlewares.GlobalExceptionMiddleware.InvokeAsync(HttpContext context) in E:\Project\HeartOrange\HeartOrange.Main\Middlewares\GlobalExceptionMiddleware.cs:line 34
0 回复 -
fate sta VIP0
2026/3/5用控制台写一个可以重现的DEMO,删掉OBJ和BIN打包上传。
0 回复 -
fate sta VIP0
2026/3/5或者升级最新试试。
0 回复 -
慢慢来、不着 VIP0
2026/3/9控制台测试项目,已删除obj和bin,用vs2026写的
0 回复 -
慢慢来、不着 VIP0
2026/3/90 回复 -
fate sta VIP0
2026/3/9[SugarColumn(IsNullable =true)] public DateOnly? Date { get; set; }建表要设置可空isnullable=true。当然也可以启用?看文档:迁移通过?启用。
表删掉重新测试
0 回复 -
慢慢来、不着 VIP0
2026/3/9@fate sta:IsNullable我在ConfigureExternalServices统一加了的,生成的数据库实体木有问题,上次我问过您,您说是null监测不到类型导致的,如果传入值不是null,就不会报错,这个问题有几个月了,您一直没处理,所以这次又提了一下。然后这次讨论的主题是另外一个bool?取值的问题,辛苦您注释掉Date这次更新,往下继续执行,就能看到问题
0 回复