title: EF Core 1.1 Soft (or logical) Delete
body: <p>I'm trying to implement Soft Delete in a .NET Core 1.1.* Web App, backed by Entity Framework Core 1.1.*. I'm using Sql Server as my DB.  </p>
  
  <p>Migrating to .NET core 2.* is not an option at the moment.  </p>
  
  <p>After reading books, tuts and 3ds, I've implemented this feature using a <code>Discriminator</code> column. The deletion procedure is apparently working as expected. What's wrong is the data retrieval: deleted entities are still shown within my EF query results.</p>
  
  <h3>Current situation</h3>
  
  <p>Here's some C# code. I'll keep things as simple as possible<br>
  The interfaces:</p>
  
  <pre><code>// Soft deletion interface    
  public intercace ISoftDeletable
  {}
  
  // Another interface for some shadow properties
  public interface IEntity
  {}
  </code></pre>
  
  <p>The base class:</p>
  
  <pre><code>public abstract class Entity : IEntity, ISoftDeletable
  {
     public int MyBaseProp { get; set; }
  }
  </code></pre>
  
  <p>One of my derived classes:</p>
  
  <pre><code>public class MyDerivedEntity: Entity
  {
     public string Name { get; set; }
  
     public IList&lt;MyChildEntity&gt; Children { get; set; }
  
  }
  
  public class MyChildEntity: Entity 
  {
      public string MyChildProp { get; set; }
  }
  </code></pre>
  
  <p>The Context</p>
  
  <pre><code>public class MyContext: DbContext
  {
     public MyContext(DbContextOptions&lt;MyContext&gt; options)
              : base(options)
     { }
  
     public DbSet&lt;MyDerivedEntity&gt; EntitiesToUse { get; set; }
  
     protected override void OnModelCreating(ModelBuilder builder)
     {
        foreach (var entity in builder.Model.GetEntityTypes())
        {                
           if (typeof(IEntity).IsAssignableFrom(entity.ClrType))
           {
             builder.Entity(entity.ClrType).Property&lt;string&gt;("MyShadowProperty");
           }
  
           if (typeof(ISoftDeletable).IsAssignableFrom(entity.ClrType))
           {
              // Discriminator column
              builder.Entity(entity.ClrType).HasDiscriminator("IsDeleted", typeof(bool)).HasValue(false);
              // Shadow Property       
              builder.Entity(entity.ClrType).Property(typeof(bool), "IsDeleted").IsRequired(true).HasDefaultValue(false);
  
              builder.Entity(entity.ClrType).Property(typeof(bool), "IsDeleted").Metadata.IsReadOnlyAfterSave = false;
           }
        }
  
        // Other model configurations
  
        base.OnModelCreating(builder);
     }
  
     // SaveChangesAsync are almost the same
     public override int SaveChanges()
     {
         AuditEntities();
  
         return base.SaveChanges();
     }
  
     private void AuditEntities()
     {
  
        foreach (EntityEntry&lt;IEntity&gt; entry in ChangeTracker.Entries&lt;IEntity&gt;())
        {
           // do something with MyShadowProperty...
        }
        foreach (EntityEntry&lt;ISoftDeletable&gt; entry in changeTracker.Entries&lt;ISoftDeletable&gt;().Where(w =&gt; w.State == EntityState.Deleted))
        {
          // Set the entity as Softly Deleted
          entry.Property("IsDeleted").CurrentValue = true;
          // Ensure the entity state is modified to prevend hard deletion
          entry.State = EntityState.Modified;
        }
     }
  }
  </code></pre>
  
  <h3>The Problem</h3>
  
  <p>Everything works as expected, except the data retrieval. 
  Here's a sample call:</p>
  
  <pre><code>var results = await _context.EntitiesToUse.Include(e =&gt; e.SomeChildEntity).AsNoTracking();
  </code></pre>
  
  <p>I expect the results to include only <em>available</em> myDerivedEntities with <code>.IsDeleted == false</code>. The problem is that my deleted entities are not filtered out. Why?<br>
  Please, what's wrong with my code? Am I missing something? </p>
  
  <p>Thank you all so much!</p>