使用HttpDelete使用C#从数据库中删除项
我正试图根据主键ID字段从数据库中删除一行。当我尝试这样做时,所有代码都会执行,没有任何错误,但该项不会从数据库中删除 我从一个有角度的前端调用将项目传递到我的C#后端,如下所示:使用HttpDelete使用C#从数据库中删除项,c#,entity-framework,sql-delete,http-delete,C#,Entity Framework,Sql Delete,Http Delete,我正试图根据主键ID字段从数据库中删除一行。当我尝试这样做时,所有代码都会执行,没有任何错误,但该项不会从数据库中删除 我从一个有角度的前端调用将项目传递到我的C#后端,如下所示: delete(customerId: number, materialCustomerId: number): Observable<Response> { return this.http.delete(`${this.getBaseUrl()}/${customerId}/materialcu
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);
}
我错过了什么 假设
等待\u customerWriter.CommitAsync(cancellationToken)
调用同一DbContext实例并调用方法SaveAsync
您应该像这样重新编写delete方法:
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();
}
公共异步任务删除(int customerId、int materialCustomerId)
{
wait _materialCustomerDeleter.DeleteAsync(MaterialCustomer.CreateWithOnlyId(materialCustomerId),HttpContext.RequestAborted);
返回Ok();
}
假设等待\u customerWriter.CommitAsync(取消令牌)
调用同一DbContext实例并调用方法SaveAsync
您应该像这样重新编写delete方法:
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();
}
公共异步任务删除(int customerId、int materialCustomerId)
{
wait _materialCustomerDeleter.DeleteAsync(MaterialCustomer.CreateWithOnlyId(materialCustomerId),HttpContext.RequestAborted);
返回Ok();
}
什么是\u context.SetModified
?为什么不在DbSet上删除
?您还应该调用SaveAsync来持久化更改。您可能还应该从web api/mvc方法返回一个结果,如200。@Igor my SetModified methodpublic virtual void SetModified(T entity),其中T:class{if(Entry(entity)(Entry)(entity.State!=EntityState.Modified)Entry(entity).State=EntityState.Modified;}
设置枚举上的EntityState,其中2=已删除,3=已修改。什么是上下文。SetModified
?为什么不在DbSet上删除?您还应该调用SaveAsync来持久化更改。您可能还应该从web api/mvc方法返回一个结果,如200。@Igor my SetModified methodpublic virtual void SetModified(T entity),其中T:class{if(Entry(entity)(Entry)(entity.State!=EntityState.Modified)Entry(entity).State=EntityState.Modified;}
设置枚举上的EntityState,其中2=已删除,3=已修改