gpt4 book ai didi

c# - 如何改进有关 EntityCollections 的代码?

转载 作者:太空宇宙 更新时间:2023-11-03 16:37:43 24 4
gpt4 key购买 nike

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

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 was unhandled.

The object could not be added to the EntityCollection or EntityReference. An object that is attached to an ObjectContext cannot be added to an EntityCollection or EntityReference that is not associated with a source object.

最佳答案

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

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

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

也许让事情正常进行的最简单方法是将您的上下文传递给 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在两个地方。

这就是你要找的,还是我理解错了?

关于c# - 如何改进有关 EntityCollections<TEntity> 的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8541539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com