8

我有两个简单的 POCO 课程;我正在尝试MyY使用Y. 我已经尝试了多种方法来做到这一点,并且认为我可能遗漏了一些明显或简单的东西。

public class X
{
     public int Id { get; set;}
     public virtual Y MyY { get; set; }
}

public class Y
{
     public int Id { get; set; }
     // ...
}

我已经通过我的构造函数的子类中的这个调用关闭了延迟加载DbContext

Configuration.LazyLoadingEnabled = false;

检索X我尝试过的

context.Set<X>.Include("MyY").FirstOrDefault(x => ....);

这没有用。我试过了

var result = context.Set<X>.FirstOrDefault(x => ....);
context.Entry(result).Reference("MyY").Load();

这有效,但需要两次往返数据库。我试过了

context.Set<X>.Select(x => new { X = x, Y = x.MyY }).FirstOrDefault(x => ...);

这也有效,但“削弱”了我的模型(通常投影到新类型并没有那么糟糕,但这些 EF POCO 的“形状”非常适合我稍后将通过 WCF 发送的 DTO)。

我终于尝试按照另一个问题的答案virtual中的建议从该MyY物业中删除,但这根本没有效果。

最后,我想使用通用存储库模式。我最终得到的是以下设计,部分显示,它支持显式加载(不是首选)和在修改为正常工作时急切加载。如何修改它以获得单个数据库往返急切负载?

public class EFRepository : IRepository
{
    public T Get<T>(Specification<T> specification) where T : class, IEntity
    {
        var result = ApplyEagerLoading(context.Set<T>()).FirstOrDefault(specification.IsMatch);
        ApplyPostQueryLoading(new List<T> { result });
        return result;
    }

    // doesn't really seem to work yet...
    private DbSet<T> ApplyEagerLoading<T>(DbSet<T> set) where T : class, IEntity
    {
        var ls = loadSpecs.GetOrAdd(typeof(T), () => new List<LoadSpec>());
        foreach (var spec in ls.Where(s => !s.ExplicitLoad))
            set.Include(spec.PropertyName);
        return set;
    }

    // works, but wrong on so many levels...
    private void ApplyPostQueryLoading<T>(IEnumerable<T> entities) where T : class, IEntity
    {
        var ls = loadSpecs.GetOrAdd(typeof(T), () => new List<LoadSpec>());
        foreach (var e in entities)
            foreach (var spec in ls.Where(s => s.ExplicitLoad))
                if (spec.IsCollection)
                    context.Entry(e).Collection(spec.PropertyName).Load();
                else
                    context.Entry(e).Reference(spec.PropertyName).Load();
    }

    private readonly IDictionary<Type, IList<LoadSpec>> loadSpecs = new Dictionary<Type, IList<LoadSpec>>();

    private class LoadSpec
    {
        internal string PropertyName;
        internal bool ExplicitLoad;
        internal bool IsCollection;
    }
}

示例用途:

// add a rule to load MyY explicitly
repository.AddLoadRule<X>(x => x.MyY, explicit:true, isCollection:false)
...
var x = repository.Get<X>(new Specification<X>(x => x.Id == 5));

// add a rule to load MyY with X
repository.AddLoadRule<X>(x => x.MyY, explicit:false)
...
// x.MyY will be null! Doesn't work!
var x = repository.Get<X>(new Specification<X>(x => x.Id == 5));

基于答案的更新:

事实证明我的临时代码示例撒了谎(上面的那些单行代码)。我实际上已经将 的结果缓存.Include在一个局部变量中,但应用了.FirstOrDefault.not.Set<X>的结果.Include。这是对 的修复ApplyEagerLoading,它反映了其他人在相关问题中的建议:

    private IQueryable<T> ApplyEagerLoading<T>(IEnumerable<T> set) where T : class, IEntity
    {
        var ls = loadSpecs.GetOrAdd(typeof(T), () => new List<LoadSpec>());
        var query = set.AsQueryable();
        return ls.Where(s => !s.ExplicitLoad).Aggregate(query, (current, spec) => current.Include(spec.PropertyName));
    }
4

1 回答 1

1

这应该有效:

X entity = context.Set<X>().Include(x => x.MyY).FirstOrDefault();

如果不是,问题一定出在其他地方。

如果您需要一些急切的加载策略,请检查此答案

于 2011-07-06T19:37:05.050 回答