C
C#

help

Why only one EF Core Global Query Filter work but others are not

MBAM B A R K9/22/2022
Hi, I'm working with EF Core 6, recently I want to use the Global Query Filters. So in OnModelCreating inside my DbContext I applied the two Gobal Query Filtersshown bellow: The Problem: The problem is only the first query filter who applied, but the IAuditable one not applied, please how do I fix this issue ? Massive thanks in advance @Database My query filters:
foreach ( var entityType in modelBuilder.Model.GetEntityTypes() )
{
if ( typeof( IArchivable ).IsAssignableFrom( entityType.ClrType ) )
{
var parameter = Expression.Parameter( entityType.ClrType , "p" );

var isArchivedProperty = MemberExpression.Property(parameter, "IsArchived");

// check isArchived == null

var isArchivedNullCheck = Expression.Equal( isArchivedProperty , Expression.Constant( null , typeof( bool ? ) ) );
var isArchivedNullFunc = Expression.Lambda( isArchivedNullCheck , parameter );

// check isArchived == false
var isArchivedFalseCheck = Expression.Equal( isArchivedProperty , Expression.Constant( false , typeof( bool ? ) ) );
var isArchivedFalseFunc = Expression.Lambda( isArchivedFalseCheck , parameter );

// Combine the two expressions using OR
var isArchivedCExpression = Expression.OrElse( isArchivedNullCheck , isArchivedFalseCheck );

// Create the lambda expression
var isArchivedFunc = Expression.Lambda( isArchivedCExpression , parameter );

modelBuilder.Entity( entityType.ClrType ).HasQueryFilter( isArchivedFunc );

}
foreach ( var entityType in modelBuilder.Model.GetEntityTypes() )
{
if ( typeof( IArchivable ).IsAssignableFrom( entityType.ClrType ) )
{
var parameter = Expression.Parameter( entityType.ClrType , "p" );

var isArchivedProperty = MemberExpression.Property(parameter, "IsArchived");

// check isArchived == null

var isArchivedNullCheck = Expression.Equal( isArchivedProperty , Expression.Constant( null , typeof( bool ? ) ) );
var isArchivedNullFunc = Expression.Lambda( isArchivedNullCheck , parameter );

// check isArchived == false
var isArchivedFalseCheck = Expression.Equal( isArchivedProperty , Expression.Constant( false , typeof( bool ? ) ) );
var isArchivedFalseFunc = Expression.Lambda( isArchivedFalseCheck , parameter );

// Combine the two expressions using OR
var isArchivedCExpression = Expression.OrElse( isArchivedNullCheck , isArchivedFalseCheck );

// Create the lambda expression
var isArchivedFunc = Expression.Lambda( isArchivedCExpression , parameter );

modelBuilder.Entity( entityType.ClrType ).HasQueryFilter( isArchivedFunc );

}
else if ( typeof( IAuditable ).IsAssignableFrom( entityType.ClrType ) )
{
var parameter = Expression.Parameter( entityType.ClrType , "p" );

var isDeletedProperty = MemberExpression.Property(parameter, "IsDeleted");

// check isDeleted == null
var isDeletedNullCheck = Expression.Equal( isDeletedProperty , Expression.Constant( null , typeof( bool ? ) ) );
var isDeletedNullFunc = Expression.Lambda( isDeletedNullCheck , parameter );

// check isDeleted == false
var isDeletedFalseCheck = Expression.Equal( isDeletedProperty , Expression.Constant( false , typeof( bool ? ) ) );
var isDeletedFalseFunc = Expression.Lambda( isDeletedFalseCheck , parameter );

// Combine the two expressions using OR
var isDeletedCExpression = Expression.OrElse( isDeletedNullCheck , isDeletedFalseCheck ); // IsDeleted == null || IsDeleted == false

// Create the lambda expression
var isDeletedFunc = Expression.Lambda( isDeletedCExpression , parameter ); // p => p.IsDeleted == null || p.IsDeleted == false

modelBuilder.Entity( entityType.ClrType ).HasQueryFilter( isDeletedFunc );
}
}
else if ( typeof( IAuditable ).IsAssignableFrom( entityType.ClrType ) )
{
var parameter = Expression.Parameter( entityType.ClrType , "p" );

var isDeletedProperty = MemberExpression.Property(parameter, "IsDeleted");

// check isDeleted == null
var isDeletedNullCheck = Expression.Equal( isDeletedProperty , Expression.Constant( null , typeof( bool ? ) ) );
var isDeletedNullFunc = Expression.Lambda( isDeletedNullCheck , parameter );

// check isDeleted == false
var isDeletedFalseCheck = Expression.Equal( isDeletedProperty , Expression.Constant( false , typeof( bool ? ) ) );
var isDeletedFalseFunc = Expression.Lambda( isDeletedFalseCheck , parameter );

// Combine the two expressions using OR
var isDeletedCExpression = Expression.OrElse( isDeletedNullCheck , isDeletedFalseCheck ); // IsDeleted == null || IsDeleted == false

// Create the lambda expression
var isDeletedFunc = Expression.Lambda( isDeletedCExpression , parameter ); // p => p.IsDeleted == null || p.IsDeleted == false

modelBuilder.Entity( entityType.ClrType ).HasQueryFilter( isDeletedFunc );
}
}
Ggerard9/23/2022
Because you can only have one global filter, if set the filter with HasQueryFilter and try to add another one with HasQueryFilter, it'll actually replace the old filter. You might want to combine the two expressions with Expression.AndAlso
UUUnknown User9/23/2022
Message Not Public
Sign In & Join Server To View
MBAM B A R K9/23/2022
But the problem is the Entities types will be differents because one filter should be applied on IAuditable entities and another filter should be applied on IArchivable entties. Is I miss something ? I'm gonna crazy with this issue hmmmmm, omg maybe I should write query filter for every entity manually
Ggerard9/23/2022
That shouldn't be an issue, the parameter in the filter is the entity itself:
var parameter = Expression.Parameter( entityType.ClrType , "p" );
var parameter = Expression.Parameter( entityType.ClrType , "p" );
which can both implement IAuditable and IArchivable. So instead of doing an "else-if", you change it to an if-statement that combines the expression. Example:
foreach ( var entityType in modelBuilder.Model.GetEntityTypes() )
{
var parameter = Expression.Parameter( entityType.ClrType , "p" );

Expression condition? = null;

if ( typeof( IArchivable ).IsAssignableFrom( entityType.ClrType ) )
{
var isArchivedProperty = MemberExpression.Property(parameter, "IsArchived");

// check isArchived == null

var isArchivedNullCheck = Expression.Equal( isArchivedProperty , Expression.Constant( null , typeof( bool ? ) ) );
var isArchivedNullFunc = Expression.Lambda( isArchivedNullCheck , parameter );

// check isArchived == false
var isArchivedFalseCheck = Expression.Equal( isArchivedProperty , Expression.Constant( false , typeof( bool ? ) ) );
var isArchivedFalseFunc = Expression.Lambda( isArchivedFalseCheck , parameter );

// Combine the two expressions using OR
condition = Expression.OrElse( isArchivedNullCheck , isArchivedFalseCheck );
}

if ( typeof( IAuditable ).IsAssignableFrom( entityType.ClrType ) )
{
var isDeletedProperty = MemberExpression.Property(parameter, "IsDeleted");

// check isDeleted == null
var isDeletedNullCheck = Expression.Equal( isDeletedProperty , Expression.Constant( null , typeof( bool ? ) ) );
var isDeletedNullFunc = Expression.Lambda( isDeletedNullCheck , parameter );

// check isDeleted == false
var isDeletedFalseCheck = Expression.Equal( isDeletedProperty , Expression.Constant( false , typeof( bool ? ) ) );
var isDeletedFalseFunc = Expression.Lambda( isDeletedFalseCheck , parameter );

// Combine the two expressions using OR
var isDeletedCExpression = Expression.OrElse( isDeletedNullCheck , isDeletedFalseCheck ); // IsDeleted == null || IsDeleted == false

condition = condition == null ? isDeletedCExpression : Expression.AndAlso( condition , isDeletedCExpression );
}

if ( condition != null )
{
var filter = Expression.Lambda( condition , parameter );
modelBuilder.Entity( entityType.ClrType ).HasQueryFilter( filter );
}
}
foreach ( var entityType in modelBuilder.Model.GetEntityTypes() )
{
var parameter = Expression.Parameter( entityType.ClrType , "p" );

Expression condition? = null;

if ( typeof( IArchivable ).IsAssignableFrom( entityType.ClrType ) )
{
var isArchivedProperty = MemberExpression.Property(parameter, "IsArchived");

// check isArchived == null

var isArchivedNullCheck = Expression.Equal( isArchivedProperty , Expression.Constant( null , typeof( bool ? ) ) );
var isArchivedNullFunc = Expression.Lambda( isArchivedNullCheck , parameter );

// check isArchived == false
var isArchivedFalseCheck = Expression.Equal( isArchivedProperty , Expression.Constant( false , typeof( bool ? ) ) );
var isArchivedFalseFunc = Expression.Lambda( isArchivedFalseCheck , parameter );

// Combine the two expressions using OR
condition = Expression.OrElse( isArchivedNullCheck , isArchivedFalseCheck );
}

if ( typeof( IAuditable ).IsAssignableFrom( entityType.ClrType ) )
{
var isDeletedProperty = MemberExpression.Property(parameter, "IsDeleted");

// check isDeleted == null
var isDeletedNullCheck = Expression.Equal( isDeletedProperty , Expression.Constant( null , typeof( bool ? ) ) );
var isDeletedNullFunc = Expression.Lambda( isDeletedNullCheck , parameter );

// check isDeleted == false
var isDeletedFalseCheck = Expression.Equal( isDeletedProperty , Expression.Constant( false , typeof( bool ? ) ) );
var isDeletedFalseFunc = Expression.Lambda( isDeletedFalseCheck , parameter );

// Combine the two expressions using OR
var isDeletedCExpression = Expression.OrElse( isDeletedNullCheck , isDeletedFalseCheck ); // IsDeleted == null || IsDeleted == false

condition = condition == null ? isDeletedCExpression : Expression.AndAlso( condition , isDeletedCExpression );
}

if ( condition != null )
{
var filter = Expression.Lambda( condition , parameter );
modelBuilder.Entity( entityType.ClrType ).HasQueryFilter( filter );
}
}
MBAM B A R K9/23/2022
@gerard sorry for annoying mate, I want to thanking you about your help and time I almost used your provided solution
foreach ( var entityType in modelBuilder.Model.GetEntityTypes() )
{
var parameter = Expression.Parameter( entityType.ClrType , "p" );
Expression ? conditions = null;


if ( typeof( IArchivable ).IsAssignableFrom( entityType.ClrType ) )
{
var isArchivedProperty = MemberExpression.Property( parameter , "IsArchived" );

// check isArchived == null
var isArchivedNullCheck = Expression.Equal( isArchivedProperty , Expression.Constant( null , typeof( bool ? ) ) );
var isArchivedNullFunc = Expression.Lambda( isArchivedNullCheck , parameter );

// check isArchived == false
var isArchivedFalseCheck = Expression.Equal( isArchivedProperty , Expression.Constant( false , typeof( bool ? ) ) );
var isArchivedFalseFunc = Expression.Lambda( isArchivedFalseCheck , parameter );

// Combine the two expressions using OR
conditions = Expression.OrElse( isArchivedNullFunc , isArchivedFalseFunc );
}
foreach ( var entityType in modelBuilder.Model.GetEntityTypes() )
{
var parameter = Expression.Parameter( entityType.ClrType , "p" );
Expression ? conditions = null;


if ( typeof( IArchivable ).IsAssignableFrom( entityType.ClrType ) )
{
var isArchivedProperty = MemberExpression.Property( parameter , "IsArchived" );

// check isArchived == null
var isArchivedNullCheck = Expression.Equal( isArchivedProperty , Expression.Constant( null , typeof( bool ? ) ) );
var isArchivedNullFunc = Expression.Lambda( isArchivedNullCheck , parameter );

// check isArchived == false
var isArchivedFalseCheck = Expression.Equal( isArchivedProperty , Expression.Constant( false , typeof( bool ? ) ) );
var isArchivedFalseFunc = Expression.Lambda( isArchivedFalseCheck , parameter );

// Combine the two expressions using OR
conditions = Expression.OrElse( isArchivedNullFunc , isArchivedFalseFunc );
}

if ( typeof( IAuditable ).IsAssignableFrom( entityType.ClrType ) )
{
var isDeletedProperty = MemberExpression.Property(parameter, "IsDeleted");

// check isDeleted == null
var isDeletedNullCheck = Expression.Equal( isDeletedProperty , Expression.Constant( null , typeof( bool ? ) ) );
var isDeletedNullFunc = Expression.Lambda( isDeletedNullCheck , parameter );

// check isDeleted == false
var isDeletedFalseCheck = Expression.Equal( isDeletedProperty , Expression.Constant( false , typeof( bool ? ) ) );
var isDeletedFalseFunc = Expression.Lambda( isDeletedFalseCheck , parameter );

// Combine the two expressions using OR
var isDeletedCExpression = Expression.OrElse( isDeletedNullCheck , isDeletedFalseCheck );

// Add the Expression to the Conditions
conditions = conditions == null ? isDeletedCExpression : Expression.AndAlso( conditions , isDeletedCExpression );
}

if ( conditions != null )
{
var filter = Expression.Lambda( conditions , parameter );
modelBuilder.Entity( entityType.ClrType ).HasQueryFilter( filter );
}
}

if ( typeof( IAuditable ).IsAssignableFrom( entityType.ClrType ) )
{
var isDeletedProperty = MemberExpression.Property(parameter, "IsDeleted");

// check isDeleted == null
var isDeletedNullCheck = Expression.Equal( isDeletedProperty , Expression.Constant( null , typeof( bool ? ) ) );
var isDeletedNullFunc = Expression.Lambda( isDeletedNullCheck , parameter );

// check isDeleted == false
var isDeletedFalseCheck = Expression.Equal( isDeletedProperty , Expression.Constant( false , typeof( bool ? ) ) );
var isDeletedFalseFunc = Expression.Lambda( isDeletedFalseCheck , parameter );

// Combine the two expressions using OR
var isDeletedCExpression = Expression.OrElse( isDeletedNullCheck , isDeletedFalseCheck );

// Add the Expression to the Conditions
conditions = conditions == null ? isDeletedCExpression : Expression.AndAlso( conditions , isDeletedCExpression );
}

if ( conditions != null )
{
var filter = Expression.Lambda( conditions , parameter );
modelBuilder.Entity( entityType.ClrType ).HasQueryFilter( filter );
}
}
the problem is when I start my ASP.net core 6 app I get this exception
InvalidOperationException: The binary operator OrElse is not defined for the types 'System.Func2[MBSM.Core.Entities.ContinuousFormation,System.Boolean]' and 'System.Func2[MBSM.Core.Entities.ContinuousFormation,System.Boolean]'.
which my ContinuousFormation is :
public class ContinuousFormation : IAuditable, IArchivable
{
// Other properties removed for clarity
public string CreatedBy { get; set; }
public DateTime CreatedOn { get; set; }
public bool IsEdited { get; set; }
public string LastEditor { get; set; }
public DateTime LastEditDate { get; set; }
public bool? IsDeleted { get; set; }
public string? DeletedBy { get; set; }
public DateTime? DeletedOn { get; set; }

public bool ? IsArchived { get; set; }
public string ArchivedBy { get; set; }
public DateTime ? ArchivedOn { get; set; }
}
public class ContinuousFormation : IAuditable, IArchivable
{
// Other properties removed for clarity
public string CreatedBy { get; set; }
public DateTime CreatedOn { get; set; }
public bool IsEdited { get; set; }
public string LastEditor { get; set; }
public DateTime LastEditDate { get; set; }
public bool? IsDeleted { get; set; }
public string? DeletedBy { get; set; }
public DateTime? DeletedOn { get; set; }

public bool ? IsArchived { get; set; }
public string ArchivedBy { get; set; }
public DateTime ? ArchivedOn { get; set; }
}
I hope if you have any idea about how do I can fix this issue ? and massive thanks in advance ooooh my gosh I just did a mistake instead of
conditions = Expression.OrElse( isArchivedNullFunc , isArchivedFalseFunc );
conditions = Expression.OrElse( isArchivedNullFunc , isArchivedFalseFunc );
I have to
conditions = Expression.OrElse( isArchivedNullCheck , isArchivedFalseCheck );
conditions = Expression.OrElse( isArchivedNullCheck , isArchivedFalseCheck );
Massive thanks bro I really appreciate your help and effort all love <3

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
.net core webapi2 API key authorizationI was wondering what the absolute best practice was to implement API key authorization for your contHow to create css file linked to a razor page ?I see that the default template have the index.razor attached with a isolated css how can we do thaXamarin - Displaying JSONI'm trying to do a simple application to get a JSON and display the values on a collection. I have aOptimizing Memory UsageI've a tool that basically takes a bunch of files and converts them to a GIF. The way it works is tC-sharp Video Extraction LibrariesI am looking for a good, preferably open-source library for importing video files (e.g. AVI) into anAPI Design of login and refresh JWT token endpoint questionsI'm designing Login and refresh token and I have several questions. This is what I return on any refSystem.CommandLine and injection of IOptions (not Option!) typeI'm trying to work out how to use the hosting and dependency injection parts of System.CommandLine tInterface that is implemented to multiple classes (different types of objects)Method in class1: ```public Employee CreateEmployee(EmployeeCreateModel1 model) { .... }``` Method Wpf how to send custom event (or RoutedEvent) from parent to all childs that implement the handler.Hi guys, as the title says, i want to send an event from parent control to all childrens that impleMinimal API's WithNameWhat does `WithName` do? ```cs app.MapGet("ftx", FtxSubscribe).WithName("FtxUserTradesSubscription"EF Core Global Query Filter ExceptionHi friends, I'm try to create a `Global query filter` for my `EF Core`, to be honest this is the firForeach vs Count with likes tableGuys I made a Vote(likes and dislikes) table. It's similar to reddit's karma system. Dislikes decreadonut.ci wonder where the mistake is in here, i've read over https://www.a1k0n.net/2011/07/20/donut-math.htAccess list when creating an accountI'm creating a login system, I need an access list in it. Who can I allow to log in and who can I reKeep getting infinity output when I dont want that [Answered]My program keeps giving infinity and I'm lost lolHide cmd output when executed from a Console app?I'm brand new to C#, am messinga round with VS -- I'm starting cmd.exe w/ some args with > outputdirPassing the where parameters by the functionHow do I get this sample code to work? Unfortunately, it does not want to work. ```cs public asyError Introducing FOREIGN KEY constraint``` An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without lc.exe exited with code -1I was creating my project with bunifu ui I went to uninstall the package from bunifu ui and now it kRun Method on other thread and call method on main thread after it finishedI have this code and want to execute everything after t.Start() as soon as it finished but without b