仅更新给定值 (Entity Framework)
Update only the given values (Entity Framework)
我正在尝试仅通过动态发送到 API 的值来更新实体,因此每次客户端发送到我的 api 不同的值时,它只会更改给定的值。
这是我的实体
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; }
}
这是我的存储库:
public Task<bool> UpdateAdmin(Administrator admin)
{
if (admin != null)
{
_context.Admins.Update(admin);
Commit();
return Task.FromResult(true);
}
else
{
return Task.FromResult(false);
}
}
假设我只想更新 phone 号码,我发送的数据只有新的 phone 号码,但其余属性也更改为 null。我怎样才能只更新给定的?
谢谢
您将需要使用 JsonPatchDocument
这是我做类似事情的示例代码
//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();
}
在 repos 中,您无需做任何复杂的事情,只需将模型作为更新传递即可,所有工作都由 json 补丁文档完成。
要了解有关补丁文档的更多信息
如果你也能用本文提供的解决方案达到同样的效果
Entity Framework validation with partial updates
更新
According to your comment, admin receives different field contents
each time.
所以可以用reflection
来dynamically
判断admin接受的各个字段的值是否为null.如果不是,则替换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);
}
}
我正在尝试仅通过动态发送到 API 的值来更新实体,因此每次客户端发送到我的 api 不同的值时,它只会更改给定的值。
这是我的实体
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; }
}
这是我的存储库:
public Task<bool> UpdateAdmin(Administrator admin)
{
if (admin != null)
{
_context.Admins.Update(admin);
Commit();
return Task.FromResult(true);
}
else
{
return Task.FromResult(false);
}
}
假设我只想更新 phone 号码,我发送的数据只有新的 phone 号码,但其余属性也更改为 null。我怎样才能只更新给定的?
谢谢
您将需要使用 JsonPatchDocument
这是我做类似事情的示例代码
//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();
}
在 repos 中,您无需做任何复杂的事情,只需将模型作为更新传递即可,所有工作都由 json 补丁文档完成。
要了解有关补丁文档的更多信息
如果你也能用本文提供的解决方案达到同样的效果 Entity Framework validation with partial updates
更新
According to your comment, admin receives different field contents each time.
所以可以用reflection
来dynamically
判断admin接受的各个字段的值是否为null.如果不是,则替换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);
}
}