首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何改进有关EntityCollections<TEntity>的这段代码?

如何改进有关EntityCollections<TEntity>的这段代码?
EN

Stack Overflow用户
提问于 2011-12-16 23:52:50
回答 1查看 509关注 0票数 2

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

代码语言:javascript
复制
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);
    }
}

如果没有,我会收到一个错误。就像这个密码一样。

代码语言:javascript
复制
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中。

EN

回答 1

Stack Overflow用户

发布于 2011-12-18 19:20:25

我希望我对你的理解是正确的.如果没有,我会更新这个答案。

首先,您的EntityCollection<Exercise> GetExercises(XElement xml)将无法工作。正如错误消息所述,您不能构建这样的随机EntityCollectionEntityCollection需要将对象附加到上下文,因为它会自动同步其列表和上下文。既然你没有说要把它附加到任何地方,它就没用了。使其工作的唯一方法是首先避免创建EntityCollection

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

也许最简单的方法是将上下文传递给GetExamProduced方法,并让它们自动地附加到上下文中。我假设它是一个常见的ObjectContext,但是您可以根据需要更新它:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8541539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档