2017-08-16 10 views
1

主キーIDフィールドに基づいてデータベースから行を削除しようとしています。私がしようとすると、すべてのコードがエラーなしで実行されますが、アイテムはデータベースから削除されません。私はこのような角度のフロントエンドの呼び出しからの私のC#のバックエンドにアイテムを渡しているHttpDeleteを使用してC#でデータベースからアイテムを削除

[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); 
    } 

delete(customerId: number, materialCustomerId: number): Observable<Response> { 
    return this.http.delete(`${this.getBaseUrl()}/${customerId}/materialcustomer/${materialCustomerId}`).catch(error => this.handleError(error)); 
} 

それはその後、私のコントローラメソッドを打ちます

最後に、私の保管方法:

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); 

    } 

私は何が欠けていますか?また

public void Delete(TblMaterialCustomer entity) 
{ 
    _context.TblMaterialCustomer.Remove(entity); 
} 

は、それはおそらくからの結果を返すために良いアイデアを次のようになります。

+0

「_context.SetModified」とは何ですか?なぜDbSetで 'Remove'をしないのですか?また、SaveAsyncを呼び出して変更を保持する必要があります。また、200のようなweb api/mvcメソッドの結果を返すべきです。 – Igor

+0

@Igor my SetModifiedメソッド 'public virtual void SetModified (Tエンティティ)T:class { if(Entry(entity).State!= EntityState.Modified) エントリ(エンティティ).State = EntityState.Modified; } 'EntityStateを列挙型に設定します.2 =削除済み、3 =修正済み – CodyCS

答えて

0

await _customerWriter.CommitAsync(cancellationToken);同じDbContextインスタンスに至るまでのコールとは、あなたがこのような削除方法を再書き込みする必要がありSaveAsyncメソッドを呼び出すと仮定すると、 WebAPIコール(OK/200のように必須ではありません)。

public async Task<IHttpActionResult> Delete(int customerId, int materialCustomerId) 
{ 
    await _materialCustomerDeleter.DeleteAsync(MaterialCustomer.CreateWithOnlyId(materialCustomerId), HttpContext.RequestAborted); 
    return Ok(); 
} 
関連する問題