2

엔티티가 있습니다. Mandate. 모든 위임에는 필수 항목이 있습니다. Person (NavigationProperty)과 많은 관계가 있습니다. (LazyLoadingEnabled, AutoDetectChangesEnabled, ValidateOnSaveEnabled, ProxyCreationEnabled와 함께) DbContext API를 사용합니다.언로드 된 탐색 속성이있는 EF 엔티티 삭제

이제 위임 엔터티를 삭제하려고합니다. 위임 엔티티는 AsNoTracking()이라는 다른 컨텍스트로로드됩니다. 나는 다음과 같은 예외가 투입 동안 지금

message.Result. 
    ObserveOn(On<DataComposition>.Scheduler). 
    Where(r => r). 
    Subscribe(_ => 
    { 
     using (var unit = UnitOfWork.Begin()) 
     { 
      var mandate = this.SelectedItem.OriginalEntity; 

      this.mandateRepository.Attach(mandate); 
      // mandate.Person.ToString(); 

      this.mandateRepository.Delete(mandate); 

      unit.Commit(); 
     } 

     this.List.RemoveOnUi(this.SelectedItem); 
    }); 

: Entities in 'CodeFirstContainer.Mandates' participate in the 'Mandate_Person' relationship. 0 related 'Mandate_Person_Target' were found. 1 'Mandate_Person_Target' is expected.

나는 인구/선택시 사람 속성을 포함 할 경우 또는 삭제 작업을 나는 속성 (lazyloading),하지만 난 DONT LIKE를 방문하는 경우 삭제 사례에 대해서만 많은 엔티티를 구체화/보유하고 있으며 DONT LIKE을 사용하면 db에 하나 이상의 DELETE 쿼리를 트리거 할 수 있습니다!

답변

2

당신이 탐색 속성을 mandate.Person 인구, 다음과 같은 SQL 문이있는 경우 ...

delete [dbo].[Mandates] 
where (([Id] = @0) and ([PersonId] = @1)) 

가 ... 데이터베이스로 전송됩니다, 사실, 내가 생각 할 것을 실제로 탐색 속성 해야에는 올바른 부모 인 PersonId이있는 사람이 채워져 있어야합니다.

나는 내가 기대했던대로 엔티티 프레임 워크 그냥 기본 키 삭제 문을 전송하지 않는 이유를 아무 생각 ...

delete [dbo].[Mandates] 
where ([Id] = @0) 

...이 없습니다. Mandate 엔티티가 Person 탐색 속성에 대한 외래 키 특성 PersonId가있는 경우

편집은

, 예상되는 SQL (위의 두 번째)가 데이터베이스로 전송됩니다. 이 경우 Person 탐색 속성은 null이고 FK 속성 값은 PersonId이 아니어도됩니다.

편집 2

당신은 아마 사람의 ID를 가져오고 그 키 더미 사람을 만들 수있을 것 FK 속성을 가장 DB-왕복-비용으로 방법을 소개하지 않으려면 메모리 :

// ... 
var personId = context.Mandates 
    .Where(m => m.Id == mandate.Id) 
    .Select(m => m.Person.Id) 
    .Single(); 

mandate.Person = new Person { Id = personId }; 

this.mandateRepository.Attach(mandate); 
this.mandateRepository.Delete(mandate); 
// ... 
관련 문제