Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更新实体的问题 #143

Closed
gesions opened this issue Nov 28, 2019 · 19 comments
Closed

更新实体的问题 #143

gesions opened this issue Nov 28, 2019 · 19 comments

Comments

@gesions
Copy link

gesions commented Nov 28, 2019

var userAuth = new UserAuth();
userAuth.F_RoleType = _model.F_RoleType;
userAuth.F_UseType = _model.F_RoleType;

var returnValue = await _freeSql.Update()
.Where(p => p.F_EmpId == _model.F_EmpId)
.SetSource(userAuth)
.ExecuteAffrowsAsync();

上面代码是更新数据,但empid不是主键。用这种方式执行会报错。

{
"messages": [
"An error ocurred."
],
"developerMessage": {
"ClassName": "System.Data.SqlClient.SqlException",
"Message": "“)”附近有语法错误。",
"Data": {
"HelpLink.ProdName": "Microsoft SQL Server",
"HelpLink.ProdVer": "11.00.3128",
"HelpLink.EvtSrc": "MSSQLServer",
"HelpLink.EvtID": "102",
"HelpLink.BaseHelpUrl": "https://go.microsoft.com/fwlink",
"HelpLink.LinkId": "20476",
"SqlError 1": "System.Data.SqlClient.SqlError: “)”附近有语法错误。"
},
"InnerException": null,
"HelpURL": null,
"StackTraceString": " at FreeSql.Internal.CommonProvider.UpdateProvider1.RawExecuteAffrowsAsync()\r\n at FreeSql.Internal.CommonProvider.UpdateProvider1.SplitExecuteAffrowsAsync(Int32 valuesLimit, Int32 parameterLimit)\r\n at Account.API.Controllers.identityController.ChangeType(frm_ChangType _model) in C:\dev\erp\src\Services\Account2.API\Controllers\identityController.cs:line 49\r\n at lambda_method(Closure , Object )\r\n at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()\r\n at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\r\n at System.Threading.Tasks.ValueTask`1.get_Result()\r\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()\r\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()\r\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)\r\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()\r\n at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextExceptionFilterAsync()",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": null,
"HResult": -2146232060,
"Source": "Core .Net SqlClient Data Provider",
"WatsonBuckets": null,
"Errors": null,
"ClientConnectionId": "e1b53e4e-fd42-418d-93c5-a24726a9b7b2"
}
}

{"code":500,"mes":"UPDATE [UserAuth] SET [F_SUId] = @p_0, [F_EmpId] = @p_1, [F_UserId] = @p_2, [F_ComId] = @p_3, [F_UseType] = @p_4, [F_OrgGuids] = @p_5, [F_RoleType] = @p_6, [F_RoleId] = @p_7 \r\nWHERE () AND ( F_EmpId='d3ec7e01-e85c-4024-b15e-d18795d2cefb' )","data":null,"record":null,"second":""}

调试是发现where里面是空的。

@2881099
Copy link
Collaborator

2881099 commented Nov 28, 2019

SetSource 适合更新整个实体,有主键的实体。

换个方法

var returnValue = await _freeSql.Update()
.Where(p => p.F_EmpId == _model.F_EmpId)
.Set(a=> new UserAuth {
F_RoleType = _model.F_RoleType,
F_UseType = _model.F_RoleType
})
.ExecuteAffrowsAsync();

@gesions
Copy link
Author

gesions commented Nov 28, 2019

SetSource 适合更新整个实体,有主键的实体。

换个方法

var returnValue = await _freeSql.Update()
.Where(p => p.F_EmpId == _model.F_EmpId)
.Set(a=> new UserAuth {
F_RoleType = _model.F_RoleType,
F_UseType = _model.F_RoleType
})
.ExecuteAffrowsAsync();

好像这样也是不行的。

@2881099
Copy link
Collaborator

2881099 commented Nov 28, 2019

你试一下,😄

@gesions
Copy link
Author

gesions commented Nov 28, 2019

SetSource 适合更新整个实体,有主键的实体。

换个方法

var returnValue = await _freeSql.Update()
.Where(p => p.F_EmpId == _model.F_EmpId)
.Set(a=> new UserAuth {
F_RoleType = _model.F_RoleType,
F_UseType = _model.F_RoleType
})
.ExecuteAffrowsAsync();

我现在只能写成这样。
var up = _freeSql.Update()
.Where(p => p.F_EmpId == _model.F_EmpId);

        up.Set(a => a.F_RoleType, _model.F_RoleType);
        up.Set(a => a.F_UseType, _model.F_RoleType);

@2881099
Copy link
Collaborator

2881099 commented Nov 28, 2019

.Set(a=> new UserAuth {
F_RoleType = _model.F_RoleType,
F_UseType = _model.F_RoleType
})

这种不成吗?

@gesions
Copy link
Author

gesions commented Nov 28, 2019

.Set(a=> new UserAuth {
F_RoleType = _model.F_RoleType,
F_UseType = _model.F_RoleType
})

这种不成吗?

不行的,连tosql()也不会出来的。

@2881099
Copy link
Collaborator

2881099 commented Nov 29, 2019

image

看这个测试

@gesions
Copy link
Author

gesions commented Nov 29, 2019

image

看这个测试

但你这个好像是id是主键的情况吧?我的并不是主键呢,只是一个条件。

@2881099
Copy link
Collaborator

2881099 commented Nov 29, 2019

不用主键也可以,只有 where 条件存在,则执行
不存在,则直接返回 0

当没有 set 子句时,也是直接返回 0

@2881099
Copy link
Collaborator

2881099 commented Nov 29, 2019

image

@gesions
Copy link
Author

gesions commented Nov 29, 2019

不用主键也可以,只有 where 条件存在,则执行
不存在,则直接返回 0

当没有 set 子句时,也是直接返回 0

你用的是sqllite的,我是mssql的,这个有关系吗?

@2881099
Copy link
Collaborator

2881099 commented Nov 29, 2019

没有关系,里面走的代码是一样的

@2881099
Copy link
Collaborator

2881099 commented Nov 29, 2019

image

这个是 sqlserver 测试的

@gesions
Copy link
Author

gesions commented Nov 29, 2019

image

这个是 sqlserver 测试的

我等下再试下,昨天试了几次的确不行。

@gesions
Copy link
Author

gesions commented Nov 29, 2019

image

这个是 sqlserver 测试的

set里面事先赋值的实体不行的。
.Set(a => userAuth)
类似这样不行,你上面的那个是可以的。

@2881099
Copy link
Collaborator

2881099 commented Nov 29, 2019

这个是表达式解析,不能直接Set实体,解不出要更新的字段

@gesions
Copy link
Author

gesions commented Nov 29, 2019

这个是表达式解析,不能直接Set实体,解不出要更新的字段

如果是Set T呢?

@2881099
Copy link
Collaborator

2881099 commented Nov 29, 2019

SetSource 更新整个实体所有字段,但是需要实体有主键值,这种情况不用给为条件,它会自动把实体的主键作为条件。

@2881099
Copy link
Collaborator

2881099 commented Nov 29, 2019

where set 任何为空,都不执行sql,安全考虑

@gesions gesions closed this as completed Dec 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants