2

这是我不相信的代码。请检查我如何将实体集合作为参数传递。

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。

4

1 回答 1

0

我希望从评论中我能正确理解你......如果不是,我会更新这个答案。

首先,你EntityCollection<Exercise> GetExercises(XElement xml)不会工作。正如错误消息所说,您不能EntityCollection像这样构造一个随机数:EntityCollection需要将对象附加到上下文,因为它会自动将其列表与上下文同步。而且由于您没有说将其附加到任何地方,因此它不起作用。使其工作的唯一方法是首先避免创建一个EntityCollection

你的void GetExercises(XElement xml, EntityCollection<Exercise> entityCollection)程序可以工作。但是,您需要确保实际上一个EntityCollection<Exercise>能够调用它的实例。您创建ExamProduced对象的方式,在您从返回时不会附加到上下文,因此此时GetExamProduced不可能拥有EntityCollection<Exercise>它的属性。

也许让事情正常工作的最简单方法是将您的上下文传递给GetExamProduced方法,并让它们自动附加到上下文。我假设它是一个 common 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两个地方。

这是你要找的,还是我误会了?

于 2011-12-18T19:20:25.180 回答