1 year ago
#389270
Ole Haugset
Net Core IgnoreQueryFilters custom implementation
I am trying to create my own implementation of Net Core's IgnoreQueryFilters() method. This because I have to perform some extra operations when Ignoring the filters.
I found the original implementation of it in efcores github project: https://github.com/dotnet/efcore/blob/main/src/EFCore/Extensions/EntityFrameworkQueryableExtensions.cs
My take on it is as follows. Currently, the only difference is the name of the method and class:
public static class SoftDeleteExtensions
{
internal static readonly MethodInfo IgnoreQueryFiltersMethodInfo = typeof(SoftDeleteExtensions).GetTypeInfo().GetDeclaredMethod(nameof(IncludeDeleted))!;
public static IQueryable<TEntity> IncludeDeleted<TEntity>(this IQueryable<TEntity> source) where TEntity : class
{
if (!(source.Provider is EntityQueryProvider))
{
return source;
}
return source.Provider.CreateQuery<TEntity>(Expression.Call(null, IgnoreQueryFiltersMethodInfo.MakeGenericMethod(typeof(TEntity)), source.Expression));
}
}
When running the following code, it runs fine:
using EnevoLeadsContext context = new EnevoLeadsContext();
List<Mission> missions = context.Missions.IgnoreQueryFilters().OrderBy(m => m.Id).Take(500).ToList();
However, when using my own extension:
using EnevoLeadsContext context = new EnevoLeadsContext();
List<Mission> missions = context.Missions.IncludeDeleted().OrderBy(m => m.Id).Take(500).ToList();
I get the following error:
System.InvalidOperationException HResult=0x80131509 Message=The LINQ expression 'DbSet() .Where(m => !(m.Deleted)) .IncludeDeleted()' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information. Source=Microsoft.EntityFrameworkCore
What is it that I'm missing?
linq
.net-core
entity-framework-core
0 Answers
Your Answer