如何改进有关EntityCollections <tentity>?</tentity>的代码

时间:2011-12-16 23:52:51

标签: c# linq entity-framework entity-framework-4 entitycollection

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

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   与源对象相关联。

1 个答案:

答案 0 :(得分:0)

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

首先,您的EntityCollection<Exercise> GetExercises(XElement xml)无效。正如错误消息所示,您无法像这样构造随机EntityCollectionEntityCollection需要将对象附加到上下文,因为它会自动将其列表与上下文同步。既然你没有说要把它贴在任何地方,它就行不通。使其工作的唯一方法是避免首先创建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

这是你正在寻找的,还是我误解了?