You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Automatically perform required conversions when query filter is defined on interface or base type #10257
Currently this throws the following exception: System.ArgumentException: 'The entity type 'MyApp.ISoftDeletableEntity' provided for the argument 'clrType' must be a reference type.'
Making a better experience around this is covered by #9117 and #6787.
The code is more complicated in this case than in many others because it requires constructing a lambda expression where the parameter is validated to be the exact entity type--not the interface. I'm going to re-purpose this bug to automatically handle base types/interfaces in EF code. After this issue is fixed it should be possible to simplify the above to:
protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){Expression<Func<ISoftDeletableEntity,bool>>expression= e =>!e.IsDeleted;foreach(varentityTypeinmodelBuilder.Model.GetEntityTypes().Where(e =>typeof(ISoftDeletableEntity).IsAssignableFrom(e.ClrType))){modelBuilder.Entity(entityType.ClrType).HasQueryFilter(expression);}}
Reacted by Nick Muller, Samuel Comino, jstynder, Sam and Pedro William B Moraes
changed the title [-]Allow model-level filters by base-type or interface.[/-][+]Automatically perform required conversions when filter is defined on interface of base type[/+]on Nov 10, 2017
changed the title [-]Automatically perform required conversions when filter is defined on interface of base type[/-][+]Automatically perform required conversions when query filter is defined on interface or base type[/+]on Nov 10, 2017
@ajcvickers Wouldn't be more efficient to set IMutableEntityType.QueryFilter like my example at #8881 (comment)? That way you don't look up every affected entity type twice. Please "Automatically perform required conversions when query filter is defined" in that manner, too.
Activity
ajcvickers commentedon Nov 10, 2017
@nphmuller This is something that can be done with bulk configuration on the mapped entity types:
Making a better experience around this is covered by #9117 and #6787.
The code is more complicated in this case than in many others because it requires constructing a lambda expression where the parameter is validated to be the exact entity type--not the interface. I'm going to re-purpose this bug to automatically handle base types/interfaces in EF code. After this issue is fixed it should be possible to simplify the above to:
[-]Allow model-level filters by base-type or interface.[/-][+]Automatically perform required conversions when filter is defined on interface of base type[/+][-]Automatically perform required conversions when filter is defined on interface of base type[/-][+]Automatically perform required conversions when query filter is defined on interface or base type[/+]HappyNomad commentedon Nov 11, 2017
@ajcvickers Wouldn't be more efficient to set
IMutableEntityType.QueryFilterlike my example at #8881 (comment)? That way you don't look up every affected entity type twice. Please "Automatically perform required conversions when query filter is defined" in that manner, too.nphmuller commentedon Nov 12, 2017
@ajcvickers
Nice. Was hoping it was possible already, but got stuck rewriting the expression. Thanks for the sample. It helps out a lot!
Totally agree with the repurpose. The sample after the fix is way easier to write.
nphmuller commentedon Nov 13, 2017
I've set up a Gist which shows my query filter use-case and the code I had to write to get it working.
https://gist.github.com/nphmuller/8891c315d79aaaf720f9164cd0f10400https://gist.github.com/nphmuller/05ff66dfa67e1d02cdefcd785661a34d
ajcvickers commentedon Oct 27, 2021
Note from triage: if we implement this, then it will be as part of general mapping configuration for all uses of an interface, tracked by #6787.