问题描述
嗨,我有一个场景,我不确定实施的最佳方式。 我想编辑一个客户,例如:
public class CustomerInput
{
[MaxLength(100)]
public string LogoPath { get; set; }
[MaxLength(50)]
[GraphQLDescription("The reference code for the customer")]
public string Reference { get; set; }
[MaxLength(128)]
[Required]
[GraphQLNonNullType]
[GraphQLDescription("The name of the customer")]
public string Name { get; set; }
}
public async Task<Customer> Edit(int id,CustomerInput customerInput)
{
Customer customer = _context.Customer.FirstOrDefault(x => x.Id == id);
if (customer == null) return null;
customer.Name = customerInput.Name;
customer.Reference = customerInput.Reference;
customer.LogoPath = customerInput.LogoPath;
await _context.SaveChangesAsync();
return customer;
}
目前我要求提供所有属性。 但是我有一个用例,我只想更新客户徽标 我拥有的唯一信息是客户 ID 和徽标 我应该如何实现我的 Input 类和编辑方法,以允许我只传递带有徽标的客户输入 如果我想用所有属性更新整个客户或只用徽标更新客户,让它工作。目前我正在考虑 2 个选项
-
制作另一个编辑方法,它只接受一个 id 和另一个新输入,如 customerInputLogo,其中只有一个徽标。那么该方法只会设置标志
公共异步任务 EditCustomerLogo(int id,CustomerInputLogo customerInput) { Customer customer = _context.Customer.FirstOrDefault(x => x.Id == id); if (customer == null) return null;
customer.LogoPath = customerInput.LogoPath; 等待 _context.SaveChangesAsync(); 回头客; } -
发送编辑请求时,只需为我未使用的属性填充空值,然后将每个属性分配包装在 if 检查是否为空
if(customerInput.Name != null) customer.Name = customerInput.Name;
有没有更简单的方法来完成我在这里所说的工作?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)