title: DbSet, ModelBuilder, and EF Navigation Properties
body: <p>I'm attempting to implement a multi-tenant application where I query the db via the tenant object, instead of directly off the context.  Before I had this: </p>
  
  <pre><code>public User GetUserByEmail(string email)
      {
          using (var db = CreateContext())
          {
              return db.Users.FirstOrDefault(u =&gt; u.Email.Equals(email, StringComparison.OrdinalIgnoreCase));
          }
      }
  </code></pre>
  
  <p>Now I have this: </p>
  
  <pre><code>public User GetUserByEmail(string email)
      {
          using (var db = CreateContext())
          {
              return _tenant.Users.FirstOrDefault(u =&gt; u.Email.Equals(email, StringComparison.OrdinalIgnoreCase));
          }
      }
  </code></pre>
  
  <p>Where Tenant is the following: </p>
  
  <pre><code>public class Tenant
  {
      public Tenant()
      {
      }
  
      [Key]
      [Required]
      public int TenantId { get; set; }
  
      public virtual DbSet&lt;User&gt; Users { get; set; }
      // etc
  }
  </code></pre>
  
  <p>Where my User model has the following: </p>
  
  <pre><code>public virtual List&lt;Tenant&gt; Tenants { get; set; }
  </code></pre>
  
  <p>And in my Context configuration, I have the following:</p>
  
  <pre><code>modelBuilder.Entity&lt;Tenant&gt;()
          .HasMany(e =&gt; e.Users)
          .WithMany()
          .Map(m =&gt;
          {
              m.ToTable("UserTenantJoin");
              m.MapLeftKey("TenantId");
              m.MapRightKey("UserId");
          });
  </code></pre>
  
  <p>But I'm running into a problem with the fact that DbSet is incompatible with the ModelBuilder above - it chokes on HasMany saying that the use of DbSet cannot be inferred from usage.  </p>
  
  <p>I played with using ICollection instead, but then in my service layer all calls to <code>_tenant.Users.Include(stuff)</code>, or <code>Find()</code>, and other db queries break.  </p>
  
  <p>Example of a service method that breaks if I use ICollection: </p>
  
  <pre><code>   public User GetUserWithInterestsAndAptitudes(string username)
      {
          using (var db = CreateContext())
          {
              return _tenant.Users.  // can't use .Include on ICollection
                  Include(u =&gt; u.Relationships).
                  Include(u =&gt; u.Interests).
                  Include(u =&gt; u.Interests.Select(s =&gt; s.Subject)).
                  Include(u =&gt; u.Interests.Select(s =&gt; s.Aptitude)).
                  FirstOrDefault(s =&gt; s.Username.Equals(username, StringComparison.OrdinalIgnoreCase));
          }
      }
  </code></pre>
  
  <p>I'm hoping there's a solution that will allow me to keep the navigation properties queryable without re-architecting my service layer.  </p>
  
  <p>One option is that I revert everything back to using the context via <code>db.Users</code>, and then add another condition to every single query <code>.Where(u =&gt; u.TenantId == _tenant.TenantId)</code> - but I'm trying to avoid this.</p>
  
  <p>Any help here would be much appreciated.</p>