我正在尝试根据主键ID字段从我的数据库中删除一行。当我尝试这样做时,所有代码都会执行而没有任何错误,但该项目不会从数据库中删除。
我将这个项目传递给我的C#后端从这样的角度前端调用:
delete(customerId: number, materialCustomerId: number): Observable<Response> {
return this.http.delete(`${this.getBaseUrl()}/${customerId}/materialcustomer/${materialCustomerId}`).catch(error => this.handleError(error));
}
然后点击我的控制器方法:
[HttpDelete]
[Route("{customerId}/materialcustomer/{materialCustomerId}")]
[AccessControl(Securable.Customer, Permissions.Delete, Permissions.Execute)]
public async Task Delete(int customerId, int materialCustomerId)
{
await _materialCustomerDeleter.DeleteAsync(MaterialCustomer.CreateWithOnlyId(materialCustomerId), HttpContext.RequestAborted);
}
机械手方法:
public async Task DeleteAsync(MaterialCustomer model, CancellationToken cancellationToken = default(CancellationToken))
{
if (model == null)
throw new ArgumentNullException(nameof(model));
await _materialCustomerDeleter.DeleteAsync(new TblMaterialCustomer { MaterialCustomerId = model.MaterialCustomerId }, cancellationToken);
if (cancellationToken.IsCancellationRequested)
return;
await _customerWriter.CommitAsync(cancellationToken);
}
最后,我的存储库方法:
public async Task DeleteAsync(TblMaterialCustomer entity, CancellationToken cancellationToken = new CancellationToken())
{
var item =
await _context.TblMaterialCustomer.FirstOrDefaultAsync(i => i.MaterialCustomerId == entity.MaterialCustomerId, cancellationToken);
if (item == null || cancellationToken.IsCancellationRequested)
return;
_context.SetModified(item);
}
我错过了什么?
答案 0 :(得分:0)
假设await _customerWriter.CommitAsync(cancellationToken);
调用同一个DbContext实例并调用方法SaveAsync
,你应该重写这样的删除方法:
public void Delete(TblMaterialCustomer entity)
{
_context.TblMaterialCustomer.Remove(entity);
}
同样,从WebAPI调用返回结果可能是个好主意,尽管不需要,例如OK / 200.
public async Task<IHttpActionResult> Delete(int customerId, int materialCustomerId)
{
await _materialCustomerDeleter.DeleteAsync(MaterialCustomer.CreateWithOnlyId(materialCustomerId), HttpContext.RequestAborted);
return Ok();
}