想象一下以下场景:我有两个处于一对多关系的类,我尝试从一个类加载所有对象,同时也加载所有关联的对象。这是这两个类的骨架结构:
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;
}
有趣的是,尽管通常有更多的对象与单个“父级”相关联,但最多只有一个“子级”始终加载到 EntitySet 中。知道我在这里做错了什么吗?