Reputation: 303
I am trying to update the entity only by the values sent to the API dynamically, so each time the client will send to my api different values it will change only the given values.
This is my entity
public class Administrator
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string Role { get; set; }
public int Phone { get; set; }
}
This is my repository:
public Task<bool> UpdateAdmin(Administrator admin)
{
if (admin != null)
{
_context.Admins.Update(admin);
Commit();
return Task.FromResult(true);
}
else
{
return Task.FromResult(false);
}
}
Lets say I want to update only the phone number, I send the data with only new phone number but the rest of the properties are changed to null aswell. How can I update only the given ones?
Thanks
Upvotes: 0
Views: 1152
Reputation: 6881
According to your comment, admin receives different field contents each time.
So you can use reflection
to dynamically
determine whether the value of each field accepted by admin is null. If not, replace the value of the corresponding field of data.
public Task<bool> UpdateAdmin(Administrator admin)
{
if (admin != null)
{
var data = _context.Admins.Find(admin.Id);
Type t = typeof(Administrator);
PropertyInfo[] propInfos = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var item in propInfos)
{
var fieldValue = item.GetValue(admin);
if (fieldValue != null)
{
item.SetValue(data, fieldValue);
}
}
_context.Admins.Update(data);
Commit();
return Task.FromResult(true);
}
else
{
return Task.FromResult(false);
}
}
Upvotes: 1
Reputation: 249
You will need to use a JsonPatchDocument
here is a sample code of me doing something similar
//get the instance of admin you want to update
var adminInstance = GetAdmin....//get your admin
//partial update -> this how you create a patch doc with C#
var patchDoc = new JsonPatchDocument<Admin>();
patchDoc.Replace(x => x.phone, "put new phone number");//for any other property do x.propertyName
patchDoc.ApplyTo(adminInstance)// apply the patch doc to the instance you want to update
await UpdateAdmin(adminInstance)//your update method look good
//for people trying to do this with an api serialize the patch doc then send it to api
var adminDto =JsonConvert.SerializeObject(patchDoc);
//this is an api example
[HttpPatch("{adminId}")]
public async Task<ActionResult> UpdateAdmin(string adminId, JsonPatchDocument<Admin> patchDocument)
{
var admin= await _adminRespository.GetAsync(admin);//get the admin you want to update
patchDocument.ApplyTo(admin); //apply patch doc to the admin
await _adminRespository.UpdateAsync(admin); //pass admin to your repo
await _adminRespository.SaveAsync();
return NoContent();
}
in the repos you dont have to do anything complicated just pass the model as an update all the work is done by the json patch doc.
To learn more about patch doc
if you can also acheive the same thing with the solution provided in this Entity Framework validation with partial updates
Upvotes: 1