2014-10-08 214 views
1

加載某些行我有類實體框架 - 在導航性能

Address 
PeopleAddress //join table with a column called current 
People   // my main table, with a IList<PeopleAddress> 

modelBuilder.Entity<People>() 
      .HasMany<PeopleAddress>(m => m.CurrentAddresses) 
      .WithRequired() 
      .HasForeignKey(m => m.PeopleId); 

我(總是)想要只載入PeopleAddress在人們那裏PeopleAddress.Current == 1 行與PeopleAddress.Current = = 0對於應用程序不是必需的。 我該如何做到這一點?這可能嗎?謝謝。

PS:我不能沒有使用Include的上下文是在很多地方被實例化,我需要做的是在OnModelCreating(DbModelBuilder modelBuilder)

回答

0

對於這個工作延遲加載應禁用給你完全控制哪些數據是加載。

在PeopleAddress表上執行查詢,並確保通過PeopleAddress中的導航屬性加載人員數據。

public virtual People People { get; set; } 

PeopleAddress.Include(pa => pa.People).Where(pa => pa.Current == 1) 
0

試試吧

context.People.Include("PeopleAddress") 
       .Select(t=> new { t, 
           PeopleAddress= t.PeopleAddress 
               .Where(p=>p.Current==1)});