title: The filter expression cannot be specified for entity type. A filter may only be applied to the root entity type in a hierarchy
body: <p>I am having trouble with this error when Adding new migration. 
  <strong>The filter expression 'e => Not(e.IsDeleted)' cannot be specified for entity type 'Babysitter'. A filter may only be applied to the root entity type in a hierarchy.</strong></p>
  
  <p>What I am doing is that I have 2 classes Babysitter and Parent that both need to be  ApplicationUsers, because they have different properties. So I made them inherit the ApplicationUser class and extended them.</p>
  
  <p>This is the ApplicationUser class.</p>
  
  <pre><code>public class ApplicationUser : IdentityUser, IAuditInfo, IDeletableEntity
  {
      public ApplicationUser()
      {
          this.Id = Guid.NewGuid().ToString();
          this.Roles = new HashSet&lt;IdentityUserRole&lt;string&gt;&gt;();
          this.Claims = new HashSet&lt;IdentityUserClaim&lt;string&gt;&gt;();
          this.Logins = new HashSet&lt;IdentityUserLogin&lt;string&gt;&gt;();
      }
  
      // Audit info
      public DateTime CreatedOn { get; set; }
  
      public DateTime? ModifiedOn { get; set; }
  
      // Deletable entity
      public bool IsDeleted { get; set; }
  
      public DateTime? DeletedOn { get; set; }
  
      public virtual ICollection&lt;IdentityUserRole&lt;string&gt;&gt; Roles { get; set; }
  
      public virtual ICollection&lt;IdentityUserClaim&lt;string&gt;&gt; Claims { get; set; }
  
      public virtual ICollection&lt;IdentityUserLogin&lt;string&gt;&gt; Logins { get; set; }
  }
  </code></pre>
  
  <p>These are the Babysitter and Parent classes.</p>
  
  <pre><code>public class Babysitter : ApplicationUser
  {
      public Babysitter()
      {
          this.Appointments = new HashSet&lt;Appointment&gt;();
          this.Comments = new HashSet&lt;Comment&gt;();
      }
  
      public string Name { get; set; }
  
      public int Age { get; set; }
  
      public Gender Gender { get; set; }
  
      public DateTime DateOfBirth { get; set; }
  
      public string ImageUrl { get; set; }
  
      public string Description { get; set; }
  
      public decimal WageRate { get; set; }
  
      public string Address { get; set; }
  
      public decimal Rating { get; set; }
  
      public ICollection&lt;Comment&gt; Comments { get; set; }
  
      public ICollection&lt;Appointment&gt; Appointments { get; set; }
  }
  
  
  
  public class Parent : ApplicationUser
  {
      public Parent()
      {
          this.Comments = new HashSet&lt;Comment&gt;();
          this.Kids = new HashSet&lt;Kid&gt;();
          this.Appointments = new HashSet&lt;Appointment&gt;();
      }
  
      public string Name { get; set; }
  
      public string ImageUrl { get; set; }
  
      public decimal Rating { get; set; }
  
      public string Address { get; set; }
  
      public ICollection&lt;Comment&gt; Comments { get; set; }
  
      public ICollection&lt;Kid&gt; Kids { get; set; }
  
      public ICollection&lt;Appointment&gt; Appointments { get; set; }
  }
  </code></pre>
  
  <p>And so when I try to Add-Migration Initial I get this error: <strong>The filter expression 'e => Not(e.IsDeleted)' cannot be specified for entity type 'Babysitter'. A filter may only be applied to the root entity type in a hierarchy.</strong></p>
  
  <p>This is the ApplicationDbContext.cs </p>
  
  <pre><code>public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser, ApplicationRole, string&gt;
  {
      private static readonly MethodInfo SetIsDeletedQueryFilterMethod =
          typeof(ApplicationDbContext).GetMethod(
              nameof(SetIsDeletedQueryFilter),
              BindingFlags.NonPublic | BindingFlags.Static);
  
      public ApplicationDbContext(DbContextOptions&lt;ApplicationDbContext&gt; options)
          : base(options)
      {
      }
  
      public DbSet&lt;Babysitter&gt; Babysitters{ get; set; }
  
      public DbSet&lt;Parent&gt; Parents { get; set; }
  
      public DbSet&lt;Comment&gt; Comments { get; set; }
  
      public DbSet&lt;Kid&gt; Kids{ get; set; }
  
      public DbSet&lt;Appointment&gt; Appointments { get; set; }
  
      public DbSet&lt;Setting&gt; Settings { get; set; }
  
      public override int SaveChanges() =&gt; this.SaveChanges(true);
  
      public override int SaveChanges(bool acceptAllChangesOnSuccess)
      {
          this.ApplyAuditInfoRules();
          return base.SaveChanges(acceptAllChangesOnSuccess);
      }
  
      public override Task&lt;int&gt; SaveChangesAsync(CancellationToken cancellationToken = default) =&gt;
          this.SaveChangesAsync(true, cancellationToken);
  
      public override Task&lt;int&gt; SaveChangesAsync(
          bool acceptAllChangesOnSuccess,
          CancellationToken cancellationToken = default)
      {
          this.ApplyAuditInfoRules();
          return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
      }
  
      protected override void OnModelCreating(ModelBuilder builder)
      {
          // Needed for Identity models configuration
          base.OnModelCreating(builder);
  
          ConfigureUserIdentityRelations(builder);
  
          EntityIndexesConfiguration.Configure(builder);
  
          var entityTypes = builder.Model.GetEntityTypes().ToList();
  
          // Set global query filter for not deleted entities only
          var deletableEntityTypes = entityTypes
              .Where(et =&gt; et.ClrType != null &amp;&amp; typeof(IDeletableEntity).IsAssignableFrom(et.ClrType));
          foreach (var deletableEntityType in deletableEntityTypes)
          {
              var method = SetIsDeletedQueryFilterMethod.MakeGenericMethod(deletableEntityType.ClrType);
              method.Invoke(null, new object[] { builder });
          }
  
          // Disable cascade delete
          var foreignKeys = entityTypes
              .SelectMany(e =&gt; e.GetForeignKeys().Where(f =&gt; f.DeleteBehavior == DeleteBehavior.Cascade));
          foreach (var foreignKey in foreignKeys)
          {
              foreignKey.DeleteBehavior = DeleteBehavior.Restrict;
          }
      }
  
      private static void ConfigureUserIdentityRelations(ModelBuilder builder)
      {
          builder.Entity&lt;ApplicationUser&gt;()
              .HasMany(e =&gt; e.Claims)
              .WithOne()
              .HasForeignKey(e =&gt; e.UserId)
              .IsRequired()
              .OnDelete(DeleteBehavior.Restrict);
  
          builder.Entity&lt;ApplicationUser&gt;()
              .HasMany(e =&gt; e.Logins)
              .WithOne()
              .HasForeignKey(e =&gt; e.UserId)
              .IsRequired()
              .OnDelete(DeleteBehavior.Restrict);
  
          builder.Entity&lt;ApplicationUser&gt;()
              .HasMany(e =&gt; e.Roles)
              .WithOne()
              .HasForeignKey(e =&gt; e.UserId)
              .IsRequired()
              .OnDelete(DeleteBehavior.Restrict);
      }
  
      private static void SetIsDeletedQueryFilter&lt;T&gt;(ModelBuilder builder)
          where T : class, IDeletableEntity
      {
          builder.Entity&lt;T&gt;().HasQueryFilter(e =&gt; !e.IsDeleted);
      }
  
      private void ApplyAuditInfoRules()
      {
          var changedEntries = this.ChangeTracker
              .Entries()
              .Where(e =&gt;
                  e.Entity is IAuditInfo &amp;&amp;
                  (e.State == EntityState.Added || e.State == EntityState.Modified));
  
          foreach (var entry in changedEntries)
          {
              var entity = (IAuditInfo)entry.Entity;
              if (entry.State == EntityState.Added &amp;&amp; entity.CreatedOn == default)
              {
                  entity.CreatedOn = DateTime.UtcNow;
              }
              else
              {
                  entity.ModifiedOn = DateTime.UtcNow;
              }
          }
      }
  }
  </code></pre>