【发布时间】:2016-09-14 12:04:21
【问题描述】:
我有两个分离的实体,它们已经急切地加载了多个其他实体。 (1 到 n 关系)。
引用分离实体(存储在 ICollection 导航属性中)的热切加载实体可以同时存在于两个分离实体中。
当我想附加两个分离的实体时,我得到一个异常,因为急切加载的实体已经附加。
这是用 cmets 解释问题的示例代码:
public ProvokeAttachException()
{
//s1 and s2 share some of their samples
sample_set s1 = GetSampleSet(1);
sample_set s2 = GetSampleSet(2);
//Do some stuff
//Attaching the sample_sets again
using(StationContext context = new StationContext())
{
// Fine
context.sample_set.Attach(s1);
//Throws Exception because some of the included samples in sample_set
//have been attached automatically with s1
context.sample_set.Attach(s2);
}
}
public sample_set GetSampleSet(int setId)
{
//Eager Loading all samples that reference sample_set (1 to n relation)
using (StationContext context = new StationContext())
{
return context.sample_set.Include("sample").FirstOrDefault(s => s.id = setId);
}
}
我怎样才能毫无例外地附加两个实体?
【问题讨论】:
-
您能否提供有关您的数据建模的更多信息。您可能需要对其进行一些重构。如果对象已经附加,则不能再次附加。
-
@PedroSouki 我知道我不能再次附加实体。我有一个新的上下文,附加 s1 和 s2 是我做的第一件事,所以它们已经不能附加了。这是因为“样本”实体被急切加载,其中一些被 s1 和 s2 共享。 Attach 以递归方式自动附加所有包含在导航属性中的引用实体。我不知道如何避免异常以及 s1 和 s2 中包含的所有实体都已附加
-
@FelixKeil 我面临类似要求的挑战。我只是想知道你是否还记得你是如何解决这个挑战的。我非常感谢你的建议。
-
@user1814131 不幸的是,我不记得我们的解决方法是什么,我也无法再访问代码库了。
标签: c# entity-framework