繁体   English   中英

实体框架-仅加载导航属性中的某些行

[英]Entity Framework - Loading only certain rows in navigational property

我有课

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.Current == 1的人中加载PeopleAddress。应用程序不需要使用PeopleAddress.Current == 0的行。 我该如何实现? 这可能吗? 谢谢。

PS:我不能使用Include因为在很多地方实例化了上下文,我需要在OnModelCreating(DbModelBuilder modelBuilder)

为此,应禁用延迟加载,以完全控制要加载的数据。

在PeopleAddress表上进行查询,并确保通过PeopleAddress中的导航属性也加载了People数据。

public virtual People People { get; set; }

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

试试吧

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM