Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Is it possible to use IgnoreQueryFilters in an explicit load? #21349

Closed
JonPSmith opened this issue Jun 20, 2020 · 5 comments
Closed

Comments

@JonPSmith
Copy link

JonPSmith commented Jun 20, 2020

This isn't a feature request, but a question. Wasn't sure where I should post questions.

I'm looking at creating a soft delete method that mimics the cascade delete. For instance if I had relationships GrandFather -> Father -> Son I soft delete GrandFather it would also soft delete the Father and Son too. This could be useful in some situations.

To do this I use explicit loading to load any relationships, e.g. for collections _context.Entry(principalInstance).Collection(navigation.PropertyInfo.Name).Load();

The soft delete works, but the un-soft delete doesn't because I need to load the relationships with IgnoreQueryFilters method in it, and I can't find a way to do that.

Can you suggest a way around this? Building a real query is doable, but quite hard to get right for all situations. The other option is to cast the _context.Entry(principalInstance).Collection(navigation.PropertyInfo.Name).Query() to the right form to apply the IgnoreQueryFilters() method.

Any wisdom greatly received.

PS. If you are interested the current version of the CascadeSoftDelService can be found here.

@ajcvickers
Copy link
Contributor

@maumar @smitpatel Do we have an overall issue tracking improvements to the usability of query filters?

@JonPSmith
Copy link
Author

Though you might like to know that I have found a way to apply a IgnoreQueryFilters() method to a explicit load. This small snippet shows how I do it - the class GenericCollectionLoader<TEntity> at the end is where the clever part lives.

I consider this issue closed, but you might like to provide a version of IgnoreQueryFilters() method for explicit loading. I doubt it will be needed much but it might help someone else.

private void LoadNavigationCollection(object principalInstance, INavigation navigation)
{
    if (_set)
        //For setting we can simple load it, as we don't want to set anything that is already set
        _context.Entry(principalInstance).Collection(navigation.PropertyInfo.Name).Load();
    else
    {
        //for undoing a soft delete we need to load the collection with a IgnoreQueryFilters method
        var navValueType = navigation.PropertyInfo.PropertyType;
        var innerType = navValueType.GetGenericArguments().Single();
        var genericHelperType =
            typeof(GenericCollectionLoader<>).MakeGenericType(innerType);

        Activator.CreateInstance(genericHelperType, _context, principalInstance, navigation.PropertyInfo);
    }
}

private  class GenericCollectionLoader<TEntity> where TEntity : class
{
    public GenericCollectionLoader(DbContext context, object principalInstance, PropertyInfo propertyInfo)
    {
        var query = context.Entry(principalInstance).Collection(propertyInfo.Name).Query();
        var collection = query.Provider.CreateQuery<TEntity>(query.Expression).IgnoreQueryFilters().ToList();
        propertyInfo.SetValue(principalInstance, collection);
    }
}

@ajcvickers
Copy link
Contributor

ajcvickers commented Jun 29, 2020

@JonPSmith We're tracking making this easier in #17347. Also, I have created an overall issue for improving the query filter experience.

@JonPSmith
Copy link
Author

Your link is broken - see #21459 for link to Improve usability of global query filters issue.

@smitpatel
Copy link
Contributor

Updated broken link.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants