Home > Mobile >  Why is the UpdateAsync method setting to null, false?
Why is the UpdateAsync method setting to null, false?

Time:10-01

I would like to update only EMAIL in the database.

NOW field values in the database

Id = 1,
Email = [email protected],
IsActiveSubscibe = true,
PurchaseToken = "sdsdsdsd",
AutoRenewing = true,
IsActiveSubscibe = true,
DataTime = DateTime.UtcNow

doing UPDATE (I want to update only email)

Models.User user = new Models.User
{
   Id = 1,
   Email = [email protected],

};
  await App.Database.UpdateUser(user);

SQLITE METHOD

public Task UpdateUser(User user)
{
  return database.UpdateAsync(user);
}

After UPDATE. NOW field values in the database

Id = 1,
Email = [email protected],
IsActiveSubscibe = false,
PurchaseToken = null,
AutoRenewing = false
IsActiveSubscibe = false,
DataTime = null

The RESULT I would like to get

Id = 1,
Email = [email protected],
IsActiveSubscibe = true,
PurchaseToken = "sdsdsdsd",
AutoRenewing = true,
IsActiveSubscibe = true,
DataTime = DateTime.UtcNow

CodePudding user response:

Here you are creating a new object

Models.User user = new Models.User
{
   Id = 1,
   Email = newEmail@site.com,

};

That only has 2 values, the Id and Email. All the other fields will be set to default: null for most of the objects, and false for the bool

So no matter what info you had before, it will be replaced.

First fetch the user (I used the ID, but it can be another field )and then modify.

Models.User userToModify= await App.Database.GetUserById(1);
userToModify.email= [email protected];
await App.Database.UpdateUser(userToModify);

Happy coding!

  • Related