これは私が確信していないコードです。エンティティコレクションのパラメータとしてどのように渡しているか確認してください。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;
}
私は取得していますエラーは以下の通りです:
InvalidOperationExceptionがで未処理でした。
オブジェクトをEntityCollectionまたは EntityReferenceに追加できませんでした。 ObjectContextに添付されたオブジェクトは、 がEntityCollectionに追加されないか、またはソースオブジェクトに関連付けられた ではないEntityReferenceに追加できません。
残念ながら私はイメージを見ることができません。受け取っているエラーは何ですか? –
InvalidOperationExceptionが処理されませんでした。オブジェクトをEntityCollectionまたはEntityReferenceに追加できませんでした。 ObjectContextにアタッチされているオブジェクトを、ソースオブジェクトに関連付けられていないEntityCollectionまたはEntityReferenceに追加することはできません。 –
なぜ 'Exercise'オブジェクトの' EntityCollection'が必要ですか?なぜ単に 'List'を使わないのですか? – Yakimych