2016-06-16 63 views
0

我正試圖在ProjectVersions列表上進行基本的熱切加載,其中每個ProjectVersion都有一個FieldValues和ChildProjects列表。當我加載ProjectVersions時,我想要將FieldValues和ChildProjects與其所有屬性一起加載,但似乎在通過每個ProjectVersion時,它仍然會觸擊數據庫以獲取這些集合(檢查sql server分析器)。任何指針都會有幫助。爲什麼不急切加載在實體框架中工作

var publishedList = Repository.Find<Project>().//a bunch of wheres and selects 

      IEnumerable<ProjectVersion> publishedList = published 
       .Include(x => x.FieldValues) 
       .Include(x => x.ChildProjects) 
       .ToList(); 

    //EDIT: the context is hidden behind a generic Repository. Below are some details: 

     public class Repository : IRepository 
      { 
       internal readonly IDataContext _context; 

       public Repository(IDataContext context) 
       { 
        _context = context; 
        _context.Committed += _context_Committed; 
        _context.Deleted += _context_Deleted; 
       } 
       public IQueryable<T> Find<T>() where T : class, IEntity 
       { 
        return _context.Repository<T>(); 
       } 
     } 

     public class EfDataContext : IDataContext 
      { 
       public IQueryable<T> Repository<T>() where T : class, IEntity 
       { 
        var table = _context.Set(typeof(T)); 
        WrappedFieldsObjectQuery<T>(table.Cast<T>().AsExpandable())); 
        return table.Cast<T>().AsExpandable(); 
       } 
     } 

    public class MsmDbContext : DbContext 
    { 
     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 

      var typesToRegister = Assembly.GetExecutingAssembly().GetTypes() 
       .Where(type => 
        type.IsClass && 
        type.BaseType.IsGenericType && 
        type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>)); 

      foreach (var config in typesToRegister.Select(Activator.CreateInstance)) 
      { 
       modelBuilder.Configurations.Add((dynamic)config); 
      } 

      base.OnModelCreating(modelBuilder); 
     } 
    } 

    public class ProjectMapping : EntityTypeConfiguration<Project> 
    { 
     public ProjectMapping() 
     { 
      HasOptional(p => p.LastChangedBy).WithMany(p => p.ProjectsChanged).WillCascadeOnDelete(); 
      HasRequired(p => p.CreatedBy).WithMany(p => p.ProjectsCreated).WillCascadeOnDelete(); 
      HasRequired(d => d.Account).WithMany(p => p.Projects).WillCascadeOnDelete(); 
      HasRequired(d => d.PinType).WithMany(p => p.Projects).HasForeignKey(p => p.PinType_Id).WillCascadeOnDelete(); 
     } 
    } 

    public static class RepositoryFactory 
     { 
      public static IRepository CreateRepository() 
      { 
       return CreateEfRepository(); 
      } 

      internal static IRepository CreateEfRepository() 
      { 
       return new Repository(new EfDataContext(new MsmDbContext())); 
      } 
     } 
+0

你能發表更詳細的查詢嗎?特別是涉及的DbContext會有幫助。 –

+0

@FlorianHaider是的,DbContext隱藏在非常通用的Repository模式後面,我會嘗試查看是否可以發佈相關代碼。 – Riz

回答

0

好吧,我看不到你的完整的查詢,但在你的意見,你寫了一些關於select。一旦您使用Select()進行自定義預測,EF將忽略Include()聲明,我的猜測是這就是爲什麼預加載無法正常工作。取而代之的Include(),儘量要加載的屬性添加到您的投影,像

Repository.Find<Project>() 
.Select(p => new { project = p, p.FieldValues, p.ChildProjects }) 
.AsEnumerable().Select(p => p.project).ToList() 

這樣的投射會照顧加載數據的,你不需要Include()

0

實際上,我通過直接使用DataContext得到了它的工作。不知怎的,知識庫正在搞砸了。

相關問題