EF 7 MANY-TO-MANY QUERY - 两个查询条件

时间:2016-06-08 15:40:43

标签: entity-framework

请问如何将其作为一个EF命令编写?它是从多对多表中选择的......

这是EF查询:

List<Branch> model = await _context.Branch.AsNoTracking()
    .Where(x => x.TenantUserBranch.Any(y => y.TenantUserId == Id))
     //.Where(a => a.Deleted == false)   This here throwing error                  
      .Include(b => b.AddressBranch)
       .Include(c => c.BusinessInfoBranch)
        .Include(d => d.TenantUserBranch)
         .OrderByDescending(u => u.UpdatedTime)
          .ToListAsync();


return model.Where(a => a.Deleted == false);  //Here a.Deleted... working..

更新1 - 我的简化实体:

public class TenantUser : IdentityUser<Guid> 
{
    public TenantUser()
    {
        this.Id = Guid.NewGuid();
    }

    [Display(Name = "Is soft deleted")]
    public bool Deleted { get; set; }

    public virtual ICollection<TenantUserBranch> TenantUserBranch { get; set; } 
}

public class Branch : BaseEntityModel
{
    public Branch()
    {
        this.Id = Guid.NewGuid();                    
    }

    [Key]
    public Guid Id { get; set; } 

    public virtual ICollection<TenantUserBranch> TenantUserBranch { get; set; }
    public virtual AddressBranch AddressBranch { get; set; }         

}


public class TenantUserBranch : BaseEntityModel  //Many to many join table
{
    public TenantUserBranch()
    {
        this.Id = Guid.NewGuid();
    }        

    [Key]
    public Guid Id { get; set; }

    public Guid TenantUserId { get; set; }
    public virtual TenantUser TenantUser { get; set; }       

    public Guid BranchId { get; set; }
    public virtual Branch Branch { get; set; }
}

我需要在TenantUserId上加载特定TenantUser的所有分支,并删除== false(我使用软删除)。 TenantUserBranch是Branch和TenantUser之间多对多的自定义连接表。

0 个答案:

没有答案