1

내 데이터베이스는 Ref_Type 탐색 속성 (및 TypeID를 참조하는 FK)이있는 엔티티 테이블로 설정됩니다. 유형 테이블에는 동일한 FK 설정 인 Ref_Department가 있습니다. 마지막으로 부서 테이블에는 Ref_Locale 및 동일한 FK 설정이 있습니다.지연 읽기 및 다중 참조

싱글 톤의 속성 인 목록에 엔티티를 저장합니다. 다음과 같이 생성됩니다.

private Singleton() 
{ 
    using (Database db = new Database()) 
    { 
     List<Entities> EntityList = db.Entities.ToList<Entities>(); 
    } 
} 

괜찮 으면 탐색 속성이 모두로드되고 그 중 하나에 액세스 할 수 있습니다. 다음과 같이 엔터티 항목을 업데이트하면 문제가 발생합니다.

public void UpdateEntity(Entities oldEnt, Entities newEnt) 
{ 
    using (Database db = new Database()) 
    { 
     Entities ent = db.Entities.Where(e => e.EntityName == oldEnt.EntityName).FirstOrDefault(); 
     ent.EntityName = newEnt.EntityName; 
     ent.EntityEmail = newEnt.EntityEmail; 
     ... 
     ent.EntityType_ID = newEnt.EntityType_ID; 
     db.SaveChanges(); 
    } 

    RefreshEntities(); 
} 

public void RefreshEntities() 
{ 
    using (Database db = new Database()) 
    { 
     db.Configuration.LazyLoadingEnabled = false; 
     db.SaveChanges(); 
     EntityList = db.Entities.Include("Ref_EntityType").Include("Ref_EntityPosition").ToList<Entities>(); 
    } 
} 

Ref_Entity는 제대로로드되지만 Ref_Entity 내에서 Ref_Department는 null입니다. 난 그냥 내 생성자, 아니 주사위처럼 db.Entities.ToList<Entities>();을 사용하여 시도했습니다. 보시다시피 LazyLoading을 사용하지 않으려했습니다. 실제로 플래그를 적용하려면 SaveChanges()를 호출해야 할 수도 있습니다. 나는 또한 시도했다 .Include ("Ref_Department")하지만 단지 엔티티에 대해 존재하지 않는다고 불평한다.

내가 Ref_Type 내가 UpdateEntity 방법을 변경하지 않을 것을 단지 동일하게 유지 것이라는 가정하에 일하고 있어요, 초기화가없는 UpdateEntity 방법에 전달하는 newEnt ...

그래서 지금은 ' 무슨 일이 벌어지고 어떻게 고칠 지에 관해서는 약간의 손실이 있습니다. 누군가 내가 잘못 가고있는 곳을 설명하거나 코드를 수정하여 문제를 해결할 수있는 방법에 대한 조언을 줄 수 있다면 큰 도움이 될 것입니다.

+0

RefreshEntities() 메서드에서 느린 로딩을 사용하도록 설정 한 경우이 문제가 발생합니까? –

+0

기본적으로 지연로드가 기본적으로 설정되어 있지만 방금 솔루션을 찾았으며 곧 게시 할 예정입니다. – Trent

답변

1

변덕스럽게도 RefreshEntities()을 다음과 같이 수정했습니다.

EntityList = db.Entities.Include("Ref_EntityPosition").Include("Ref_EntityType"). 
    Include("Ref_EntityType.Ref_Department"). 
    Include("Ref_EntityType.Ref_Department.Ref_Locale").ToList<Entities>(); 

이제 모든 참고 사항이 표시됩니다.

나는 여전히 생성자에서 모든 참조를로드 할 수 있지만 호출이 동일해도 RefreshEntities() 메서드는로드 할 수 있는지 확실하지 않지만이 문제를 해결하므로 충분히 남겨 두었습니다. .

+1

참고 : [System.Data.Entity의 확장 메서드] (http://msdn.microsoft.com/en-us/library/gg671236%28v=vs.103%29.aspx) :'db.Entities . 포함 (e => e.NavigationProperty). 포함 (e => e.NavigationProperty) ' – abatishchev

+0

오! 우수한! 감사 :) – Trent