2012-05-08 4 views
1

2 개의 엔티티 유형 구매자 및 구매자 주문이 있습니다. 구매자는 여러 구매자 주문을 포함 할 수 있으며 구매자 주문 목록은 사용자가 수정할 수 있습니다. 좀 BuyerOrders를 추가 할 때 나는 메서드를 호출 :Linq2Sql의 하위 엔티티를 추가 할 수 없습니다.

private static void CreateOrdersForBuyer(Buyer buyer, int[] selectedLeadTypes) 
{ 
    foreach (var order in selectedLeadTypes.Select(leadTypeId => new BuyerOrder 
     { 
      Buyer = buyer, 
      OfferTypeID = (int) OfferTypes.Referral, 
      Price = 1, 
      RegistrationDate = DateTime.Now, 
      StatusID = (int) BuyerOrderStatus.Pending, 
      LeadtypeID = leadTypeId, 
      Profit = 1 
     })) 

    { 
     buyer.BuyerOrders.Add(order); 
    } 
} 

그리고 구매자 저장 : 나는 데이터베이스에 새로운 BuyerOrders을 볼 그 후

public virtual void Save<T>(T value) where T : class 
{ 
    //LegalLeadsDataContext.Instance = null; 
    LegalLeadsDataContext.Instance.GetTable<T>().Attach(value); 
    LegalLeadsDataContext.Instance.Refresh(RefreshMode.KeepCurrentValues, value); 
    LegalLeadsDataContext.Instance.SubmitChanges(); 
} 

:

GenericRepository.Instance.Save(buyer); 

저장 방법 . 내가 도대체 ​​뭘 잘못하고있는 겁니까? MSDN documentation

KeepCurrentValues ​​에서

답변

1

: 데이터베이스에서 검색 한 값을 원래의 값을 교환 할 수있는 새로 고침 방법을 강제로. 현재 값이 수정되지 않았습니다. KeepChanges

KeepChanges 반면

는 : 변경되었습니다 현재 값을 유지하기 위해 새로 고침 방법을 강제하지만, 데이터베이스 값과 다른 값을 업데이트합니다.

또한 Attach 메서드는 "수정 된대로"지정할 수있는 오버로드를 사용합니다.

그래서 코드에서, 아마도 당신이

public virtual void Save<T>(T value) where T : class 
{ 
    //LegalLeadsDataContext.Instance = null; 
    LegalLeadsDataContext.Instance.GetTable<T>().Attach(value, true); 
    LegalLeadsDataContext.Instance.Refresh(RefreshMode.KeepChanges, value); 
    LegalLeadsDataContext.Instance.SubmitChanges(); 
} 
이상한
+0

,하지만 효과를 변경해야 할 것으로 생각된다. 나는이 게시물 http://stackoverflow.com/questions/1636618/linq-2-sql-insert-objects-with-associated-child-objects-list-into-database-s에서 해결책을 시도했지만 내가 저장할 때 새로운 BuyerOrder 엔티티가 새 구매자를 만들지 만 모든 foreign 키를 올바르게 설정합니다. –

+0

@ valerii.sverdlik : 업데이트 된 답변을 확인하십시오. 엔티티가 연결될 때 "수정 된"수 있도록 Attach 메서드를 시도하십시오. – Jim

+0

@ valerii.sverdlik : GetTable () .Attach (value, true) 메서드를 아직 사용해 보지 못하셨습니까? – Jim

관련 문제