问题描述
我刚刚开始使用Entity Framework而不是我正常的NHiberante来查看EF的工作原理,到目前为止我遇到了很多问题,特别是分离对象并保留相关的子对象。
我买了O'Reilly实体框架书,它告诉你你实际框架默认不保留对象图,当你分离,但它不显示如何保持图形!谢谢O'Reilly真的很有用。
I've just started using Entity Framework rather than my normal NHiberante to see how EF works and so far I'm having lots of problems but one in particular is detaching an object and keeping the related child objects.
I bought the O'Reilly Entity Framework book which tells you "yes entity framework by default doesn't keep the object graph when you detach" but it doesn't show how you how to keep the graph! Thanks O'Reilly thats really useful.
无论如何,如果有人可以帮助那将是伟大的,代码如下:
Anyway if anyone can help that would be great, code below:
using (var creativeWorkshopEntities = new CreativeWorkshopEntities())
{
var q = from c in creativeWorkshopEntities.Job.Include("Files")
where c.Id == jobId
select c;
var job = q.First();
creativeWorkshopEntities.Detach(job);
return job;
}
谢谢!
Dan
推荐答案
尝试使用NoTracking查询。这样一来,这些对象就没有附加,所以你不需要分离,当图形被切割时:
Try using a NoTracking query instead. That way the objects arenever attached, so you don't need to 'detach' which is when the graph is shredded:
ie
using (var creativeWorkshopEntities = new CreativeWorkshopEntities())
{
creativeWorkshopEntities.Job.MergeOption = MergeOption.NoTracking;
var q = from c in creativeWorkshopEntities.Job.Include("Files")
where c.Id == jobId
select c;
var job = q.First();
return job;
}
希望这有帮助
Alex
(项目经理实体框架小组)
(Program Manager Entity Framework Team)
这篇关于实体框架 - 分离并保留相关对象图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!