2

이것은 내가 확신하지 못하는 코드입니다. 엔티티 컬렉션 매개 변수로 전달하는 방법을 확인하십시오.EntityCollections에 대한이 코드를 향상시키는 방법 <TEntity>?

public ExamProduced GetExamProduced(XElement xml) 
{ 
    var examProduced = new ExamProduced 
    { 
     ExamProducedID = (int)xml.Attribute("ExamID"), 
     Date = (DateTime)xml.Attribute("Date"), 
     Seed = (int)xml.Attribute("Seed"), 
     //Exercises = GetExercises(xml) 
    }; 

    GetExercises(xml, examProduced.Exercises); 
    return examProduced; 
} 

public void GetExercises(XElement xml, EntityCollection<Exercise> entityCollection) 
{ 
    var objs = 
     from objective in xml.Descendants("Objective") 
     where (bool)objective.Attribute("Produced") 
     let id = (int)objective.Attribute("ID") 
     let id2 = (Objective)entityService.Objectives.Where(o => o.ObjectiveID == id).FirstOrDefault() 
     select new Exercise 
     { 
      Objective = id2, 
      MakeUp = ... 
      Quantify = ... 
      Score = ... 
     }; 

    foreach (var exercise in objs) 
    { 
     entityCollection.Add(exercise); 
    } 
} 

그렇지 않은 경우 오류가 표시됩니다. 이 코드와 마찬가지로.

public ExamProduced GetExamProduced(XElement xml) 
{ 
    var examProduced = new ExamProduced 
    { 
     ExamProducedID = (int)xml.Attribute("ExamID"), 
     Date = (DateTime)xml.Attribute("Date"), 
     Seed = (int)xml.Attribute("Seed"), 
     Exercises = GetExercises(xml) 
    }; 

    return examProduced; 
} 

public EntityCollection<Exercise> GetExercises(XElement xml) 
{ 
    var objs = 
     from objective in xml.Descendants("Objective") 
     where (bool)objective.Attribute("Produced") 
     let id = (int)objective.Attribute("ID") 
     select new Exercise 
     { 
      ExerciseID = id, 
      MakeUp = (bool)objective.Attribute("MakeUp"), 
      Quantify = (byte)(int)objective.Attribute("Quantify"), 
      Score = (float)objective.Elements().Last().Attribute("Result") 
     }; 

     var entityCollection = new EntityCollection<Exercise>(); 

     foreach (var exercise in objs) 
      entityCollection.Add(exercise); 

     return entityCollection; 
} 

enter image description here

내가 점점 오전 오류

은 다음과 같습니다 :

경우 InvalidOperationException이 처리되지 않은했다.

개체를 EntityCollection 또는 EntityReference에 추가 할 수 없습니다. ObjectContext에 연결된 개체는 을 EntityCollection에 추가 할 수 없으며 원본 개체와 연결된 이 아닌 EntityReference에 추가 할 수 없습니다.

+0

이미지가 보이지 않습니다. 오류는 무엇입니까? –

+0

InvalidOperationException 처리되지 않았습니다. 개체를 EntityCollection 또는 EntityReference에 추가 할 수 없습니다. ObjectContext에 첨부 된 객체는 소스 객체와 관련되지 않은 EntityCollection 또는 EntityReference에 추가 될 수 없습니다. –

+0

왜'Exercise' 객체의'EntityCollection'이 필요한가요? 왜 그냥'List'를 사용하지 않는가? – Yakimych

답변

0

나는 의견에서 나는 당신을 올바르게 이해하고 있기를 바랍니다. 그렇지 않다면, 나는이 대답을 업데이트 할 것입니다.

먼저 EntityCollection<Exercise> GetExercises(XElement xml)이 작동하지 않습니다. 오류 메시지가 말하듯이 EntityCollection과 같이 임의로 구성 할 수 없습니다. EntityCollection은 컨텍스트에 자동으로 목록을 동기화하기 때문에 컨텍스트에 첨부 할 개체가 필요합니다. 그리고 당신은 어디서나 그것을 부착 할 말을하지 않기 때문에, 그것은 작동하지 않을 것입니다. 처음 작동하게하는 유일한 방법은 EntityCollection을 작성하지 않는 것입니다.

void GetExercises(XElement xml, EntityCollection<Exercise> entityCollection) 절차가 적용될 수 있습니다. 그러나 실제로 EntityCollection<Exercise> 인스턴스를 호출 할 수 있도록해야합니다. ExamProduced 개체를 만드는 방법은 GetExamProduced에서 반환 할 때까지 컨텍스트에 연결되지 않으므로 해당 시점에 EntityCollection<Exercise> 속성을 사용할 수 없습니다.

아마도 가장 쉬운 방법은 컨텍스트를 GetExamProduced 메서드에 전달하고 컨텍스트에 자동으로 첨부되도록하는 것입니다. 나는 그것이 일반적인 ObjectContext입니다 있으리라 믿고있어,하지만 필요에 따라 당신은 그것을 업데이트 할 수 있습니다

public ExamProduced GetExamProduced(XElement xml, YourContext context) 
{ 
    var examProduced = new ExamProduced() 
    { 
     ExamProducedID = (int)xml.Attribute("ExamID"), 
     Date = (DateTime)xml.Attribute("Date"), 
     Seed = (int)xml.Attribute("Seed") 
    }; 
    context.ExamsProduced.Attach(examProduced); 
    LoadExercises(xml, context, examProduced); 
    // examProduced.Exercises should be available at this point 
    return examProduced; 
} 

public void LoadExercises(XElement xml, YourContext context, ExamProduced examProduced) 
{ 
    foreach (var exercise in 
     from objective in xml.Descendants("Objective") 
     where (bool)objective.Attribute("Produced") 
     let id = (int)objective.Attribute("ID") 
     let id2 = (Objective)entityService.Objectives.Where(o => o.ObjectiveID == id).FirstOrDefault() 
     select new Exercise 
     { 
      ExamProduced = examProduced, 
      Objective = id2, 
      MakeUp = ... 
      Quantify = ... 
      Score = ... 
     })) 
    { 
     context.Exercises.Attach(exercise); 
    } 
} 

이 데이터베이스에 추가되어야 새로운 객체 인 경우 나도 몰라, 또는 이러한 개체가 존재할 것으로 예상되는 경우 이미 데이터베이스에 있습니다. 나는 후자를 추측했다. 전자의 경우 .Attach을 두 위치에서 .AddObject으로 업데이트해야합니다.

이게 당신이 찾고있는 것입니까, 아니면 오해 했습니까?

+0

그래, 그게 내가 원하는거야. Attach()가 작동하지 않아 Add()로 수정되었습니다. 첨부 기능을 사용하는 경우 다음과 같은 예외가 발생합니다.이 끝과 연관된 소스 개체가 추가, 삭제 또는 분리 상태 인 경우 연결은 유효한 작업이 아닙니다. NoTracking 병합 옵션을 사용하여로드 된 개체는 항상 분리됩니다. – Darf

+0

그 말은'context.Exercises.Attach'가 아니라'examProduced.Exercises.Attach'를 호출하고 있음을 의미합니다. – hvd