想象一下以下场景:我在一对多关系中有两个类,我尝试从一个类加载所有对象,同时也加载所有关联的对象。以下是这两个类的骨架结构:
public class Parent
{
//... id and other members omitted
[Association(Name = "FK_Parent_Children", Storage = "_entries", ThisKey = "Id")]
public EntitySet<Child> Children {
get { return _children; }
}
private readonly EntitySet<Child> _children = new EntitySet<Child>();
}
public class Child
{
//... id and other members omitted
[Column]
internal int? _belongId;
private EntityRef<Parent> _belong;
[Association(Name = "FK_Parent_Children", Storage = "_belong", ThisKey = "_belongId", OtherKey = "Id", IsForeignKey = true)]
public Parent Belong
{
get { return _belong.Entity; }
set { _belong.Entity = value; }
}
}
这里是我尝试加载所有对象的代码:
using(var context = new MyContext())
{
context.DeferredLoadingEnabled = false;
var options = new DataLoadOptions();
options.AssociateWith<Parent>(c => c.Children.OrderByDescending(x => x.Born).Take(100));
options.LoadWith<Parent>(m => m.Children);
context.LoadOptions = options;
var parents = context.Parents.OrderBy(m => m.LastName).ToList();
return parents;
}
有趣的是,最多只有一个“Child”被一直加载到EntitySet中,尽管通常有更多的对象与单个“Parent”相关联。知道我在这里做错了吗?
答案 0 :(得分:0)
我从来没有找到导致这种不稳定行为的原因,最后放弃了这样做。