kr_v
kr_v

Reputation: 139

Global Query Filter only for GET method

I have a table:

    [Table("Employee")]  
    public class Employee  
    {  
        [Key]  
        public int Id { get; set; }  
        public string Name { get; set; }  
        public bool IsDeleted { get; set; }  
    }  

And I added soft-delete attribute "IsDeleted". For it I added filter so the soft-deleted data is not showing up when I do GET all employees request. So far it works fine, I get list of all users that are not "IsDeleted" and I can make a request to delete a employee, and then make a get request, and the deleted user is not in the received list. Here is the filter:

protected override void OnModelCreating(ModelBuilder modelBuilder)  
{  
    modelBuilder.Entity<Employee>()  
        .HasQueryFilter(p => !p.IsDeleted);  

    base.OnModelCreating(modelBuilder);  
}  

Now the main problem is - how do I make this filter work only with GET request? Because currently, I am only able to set "isDeleted" = true, but I can't restore employee (change "isDeleted" to "false"). Nothing can be done with employees that are soft-deleted.

Upvotes: 2

Views: 267

Answers (1)

DavidG
DavidG

Reputation: 119056

If you want to work with a DbSet that has a query filter, but need to access objects that have been filtered, you can use the IgnoreQueryFilters method, for example:

var deletedUsers = context.Employees
    .IgnoreQueryFilters()
    .Where(e => e.IsDeleted);

Upvotes: 2

Related Questions