I am creating an API for my Xamarin Android application and I have created this method to update password in the database:
[HttpPut]
[ActionName("updatepassword")]
public HttpResponseMessage updatepassword(string password,string email)
{
user_table user = dbe.user_table
.Where(x => x.email_address == email)
.FirstOrDefault();
user.password_hash = password.GetHashCode();
var response = dbe.SaveChanges();
string finalresponse = "password updated successfully" + response;
return Request.CreateResponse(HttpStatusCode.OK, finalresponse );
}
But when I test it using postman it shows that 0 entities have been affected.
I also tried using
dbe.user_table.AddorUpdate()
but it doesn't work either, please help me.
This is my postman query
http://192.168.10.9:8044/api/account/updatepassword/?password=asad1234&email=asadregards@gmail.com
I have published my API on IIS Server that is why I am using IP Address instead of localhost.
Change your code to this:
var user = dbe.user_table
.Where(x => x.email_address == email)
.FirstOrDefault();
var response=0;
if (user!=null)
{
user.password_hash = password.GetHashCode();
dbe.Entry(user).State = EntityState.Modified;
// Or you can try
//dbe.Entry(user).Property(i => i.password_hash).IsModified = true;
response = dbe.SaveChanges();
}
........
........
And maybe it's a good idea to change you API header too:
[Route("updatepassword/{password}/{email}")]
public IHttpActionResult updatepassword(string password,string email)
{
.........
.......
return response>0 ? Ok():BadRequest();
}
It's not at all clear why marking the entire
user
object asModified
would solve this issue. It's less efficient because in the original code onlyuser.password_hash
should be marked as modified, and updated. And if the original code doesn't work, there's no reason to believe that this answer would really solve the issue.As you know DB can't update just one field. It always updates the whole record, or sometimes the whole pages of records.
Physical aspects of database updates are irrelevant here. Logically only one fields needs to be updated. What matters most here is that the issue isn't clarified. If it works now it's probably because they changed more than just this.
I've made some changes. Check my answer pls.