2012-04-30 3 views
0

DB 테이블의 요소를 삭제하는 메서드를 구현하려고합니다.EntityFrameWork를 사용하여 Db 요소를 삭제하십시오.

다음 코드는 예외를 throw하지 않지만 요소가 실행 된 후에 테이블 내에 여전히 남아 있습니다.

namespace SuitTest_tt_content_2 
{ 
    class DBUtils 
    { 
     public static StandardResponse checkContent(long id) 
     { 
      using (OnlineRedesignIndexEntities DB = new OnlineRedesignIndexEntities()) 
      { 
       try 
       { 
        Console.WriteLine("   ************** TEST ADD_CONTENT **************   "); 
        var ContentId = (from elemento in DB.Contenuto where elemento.PK_Content_ID == id select elemento).First(); 

        if (ContentId.PK_Content_ID==id) 
        { 
         DB.Attach(ContentId); 
         DB.DeleteObject(ContentId); 
         DB.Detach(ContentId); 
        } 
        else 
        { 
         throw new Exception("Errore nel reperimento dell'elemento"); 
        } 
       } 
       catch (Exception e) 
       { 
        return new StandardResponse() { Success = false, Message = e.Message }; 
       } 

       try 
       { 
        DB.SaveChanges(); 
       } 
       catch (Exception e) 
       { 
        throw new Exception("Errore nel salvataggio modifiche sul DB." + e.Message); 
       } 
      } 

      return new StandardResponse() { Success = true }; 
     } 
    } 
} 

답변

3

DB.SaveChanges()가 누락되었습니다. StackExchange.Com에서 이런 유형의 질문을하는 것이 가장 좋습니다.

0

분리 호출을 제거하십시오. SaveChanges를 수행하면 컨텍스트에서 추적중인 모든 객체를 찾습니다. 당신이 Detach를했기 때문에 더 이상 그 인스턴스를 추적하지 않습니다.

관련 문제