Home > Net >  Net Core Web API update type PUT API to how to carry out part of the update? (using SqlSugar)
Net Core Web API update type PUT API to how to carry out part of the update? (using SqlSugar)

Time:01-22

Everybody is good, at the moment there is a Web Mehod to update the user information, user information structure is as follows:

 
{
"UID" : 0,
"RiDs" : [
0
],
"ULoginName" : "string",
"ULoginPWD" : "string",
"URealName" : "string",
"UStatus" : 0,
"URemark" : "string",
"UCreateTime" : "the 2021-01-21 T02:42:34. 831 z,"
"UUpdateTime" : "the 2021-01-21 T02:42:34. 831 z,"
"ULastErrTime" : "the 2021-01-21 T02:42:34. 831 z,"
"UErrorCount" : 0,
"Name" : "string",
"Sex" : 0,
"Age" : 0,
"Birth" : "the 2021-01-21 T02:42:34. 831 z,"
"Addr" : "string",
"TdIsDelete" : true,
"RoleNames" : [
"String"
]
}


 public async Task Put ([FromBody] sysUserInfo sysUserInfo) 


But to update the case, uCreateTime uUpdateTime, uLastErrTime don't should be by the user to fill in, even if I will be the three parameters are removed from the json, but because of receiving sysUserInfo will still have the three parameters, but may not value for the right time, should be method can achieve automatic judging which parameters is the require, which is optional, and update only part of the data...

Now Add and Put in the same Class (sysUserInfo) to receive the data, I have to create a specifically for the use of the Put update the Class?

Pleas board great god gives guidance or keyword query, thank you,

CodePudding user response:

Whether to set up based on the SqlSugar update only which fields
 using (SqlSugarClient db=new SqlSugarClient (new ConnectionConfig () 
{
The ConnectionString=Server="XXXXX",
DbType=DbType. Essentially,
IsAutoCloseConnection=true,
InitKeyType=InitKeyType. Attribute
}))
{
//the Where By Expression
Var test=db. Updateable (it=& gt; New sysUserInfo () {uLoginName=", "uLoginPWD=" ", uRealName=""}) Where (=it & gt; It. The uID==0). ExecuteCommand ();
}

CodePudding user response:

reference 1/f, shawn xia response:
whether to set up based on the SqlSugar update only which fields
 using (SqlSugarClient db=new SqlSugarClient (new ConnectionConfig () 
{
The ConnectionString=Server="XXXXX",
DbType=DbType. Essentially,
IsAutoCloseConnection=true,
InitKeyType=InitKeyType. Attribute
}))
{
//the Where By Expression
Var test=db. Updateable (it=& gt; New sysUserInfo () {uLoginName=", "uLoginPWD=" ", uRealName=""}) Where (=it & gt; It. The uID==0). ExecuteCommand ();
}



Thanks for sharing, I found the following another way at present, there are wrapped ~
 public async Task The Update (
TEntity entity,
List LstColumns=null,
List LstIgnoreColumns=null,
String strWhere=""
)
{
IUpdateable The up=_db. Updateable (entity);
If (lstIgnoreColumns!=null & amp; & LstIgnoreColumns. Count & gt; 0)
{
Up=up. IgnoreColumns (lstIgnoreColumns ToArray ());
}
If (lstColumns!=null & amp; & LstColumns. Count & gt; 0)
{
Up=up. UpdateColumns (lstColumns ToArray ());
}
if (! String. IsNullOrEmpty (strWhere))
{
The up=up. The Where (strWhere);
}
Return await up. ExecuteCommandHasChangeAsync ();
}


Would like to ask how fast classification sysUserInfo inside information and save to a different List?
I'm going to match [Required], while [StringLength].. Etc. To limit, if the user didn't fill in the information, when they receive is null, want to borrow from null to determine whether the information users want to update,
Glad to trouble again, thank you.
  • Related