Ido
Ido

Reputation: 188

Disable softDelete Query filters for navigation properties

I use Ef Core 2.1, in which I enabled a soft-delete query filter.

In some cases, I want to retrieve from an entity, a soft-deleted navigation property, but I could not retrieve the data (The navigation property is null because it was soft deleted).

I used this doc (which was written in 2017) as a reference, and it is stated that

Filters cannot contain references to navigation properties.

I wish to know If there is any way to enable such behaviour.

public class Form {

    public int Id { get; set; }

    public virtual Sprint Sprint {get; set;}
}

public class Sprint: ISoftDeleteable {

    public int Id { get; set; }

    public string Name {get; set;}
}

// Indicates that every model that implements this interface should use soft delete.
public interface ISoftDeleteable
{ 

}

 // Both statements have returned null.
 Sprint sprint = applicationDbContext.Forms.FirstOrDefault(f => f.Id == 1).Sprint;
 Sprint sprint = applicationDbContext.Forms.IgnoreQueryFilters().FirstOrDefault(f => f.Id == 1).Sprint;

As a side note, I wish to state that I use a lazy loading proxy in StartUp.cs

services.AddDbContext<ApplicationDbContext>(options => 
    options.UseLazyLoadingProxies().UseSqlServer(connectionString));

Instead of using using 'Include()' and 'ThenInclude()', Because my model, is more complex than the exmplle given here. Using include will make the code more complicated and unmaintainable.

Upvotes: 4

Views: 5668

Answers (1)

Farhad Zamani
Farhad Zamani

Reputation: 5861

try this

var data = DbContext.Set<Table>().IgnoreQueryFilters().ToList();

or

var data = DbContext.TableName.IgnoreQueryFilters().ToList();

Upvotes: 7

Related Questions