Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 394
Ido Avatar asked Oct 21 '25 12:10

Ido


1 Answers

try this

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

or

var data = DbContext.TableName.IgnoreQueryFilters().ToList();
like image 133
Farhad Zamani Avatar answered Oct 23 '25 00:10

Farhad Zamani