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に2か所で更新する必要があります。

これはあなたが探しているものですか、誤解しましたか?

+0

ええ、それは私が探していたものです。 Attach()が私のために働いていない、それはAdd()によって修正されました。 Attach関数を使用すると、次のようなexeptionが生成されます。Attendは、この関連エンドに関連付けられたソースオブジェクトが追加、削除、またはデタッチ状態のときに有効な操作ではありません。 NoTrackingマージオプションを使用してロードされたオブジェクトは、常に切り離されます。 – Darf

+0

これは 'context.Exercises.Attach'ではなく' examProduced.Exercises.Attach'を呼び出すことを示唆していますが、あなたがやっているやり方もうまくいくはずです。 – hvd

関連する問題