2010-02-11 6 views
1

저는 LINQ Entity Framework를 사용하고 있으며 절차를 사용하여 여러 작업을 수행하기 전에 새로 삽입 된 ID 레코드에 액세스해야하는 시나리오를 보았습니다. 다음은 코드 sinppet이다 : 다음 함수에SaveChanges 메서드가 호출되기 전에 새로 삽입 된 ID에 액세스하는 중

public void SaveQuote(Domain.Quote currentQuote) 
    { 
     try 
     { 
      int newQuoteId; 
      //Add quote and quoteline details to db 
      if (currentQuote != null) 
      { 
       using (QuoteContainer quoteContainer = new QuoteContainer()) 
       { 
        **quoteContainer.AddToQuote(currentQuote);** 

        newQuoteId = currentQuote.QuoteId; 
       } 
      } 
      else return; 

      // Execution of some stored Procedure by using above newly generated QuoteId 

     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
    } 

quoteContainer.SaveChanges();가 호출되어 DB 변경 사항을 커밋합니다.

위의 접근 방법이 올바른지 제안 할 수 있습니까?

답변

1

지금까지 올바른.
기억 : 삽입이 발생할 때까지 IDENTITY을 얻을 수 없습니다! 업데이트에서 엔티티는 이미 IDENTITY (주로 PK)

을 보유하고 있습니다.
관련 문제