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

Cannot be converted to type error when trying to use global filter with interface #74

Closed
sw-ch opened this issue Nov 6, 2016 · 5 comments

Comments

@sw-ch
Copy link

sw-ch commented Nov 6, 2016

I tried to setup a global filter to implement soft delete filtering accross all my entity types in the progress of migrating a project to NET Core.

Used versions:
Microsoft.EntityFrameworkCore 1.0.1
Z.EntityFramework.Plus.EFCore 1.4.9

The entity inheritance tree looks like this:
Interface: IEntityBase -> Abstract class: EntityBase -> Entity classes: Address, Offers...

When I try to initialize a global filter:
QueryFilterManager.Filter<IEntityBase>("IsActive", pe => pe.Where(e => e.State == null)); QueryFilterManager.InitilizeGlobalFilter(ctx);

I get the following error:

`System.ArgumentException was unhandled
HResult=-2147024809
Message=Object of type 'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable'1[Tst.IEntityBase]' cannot be converted to type 'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable'1[Test.Entities.Address]'.
Source=System.Private.CoreLib
StackTrace:
at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
at Z.EntityFramework.Plus.QueryFilterSet.UpdateInternalQuery(DbContext context, Object query)
at Z.EntityFramework.Plus.QueryFilterQueryable'1.UpdateInternalQuery()
at Z.EntityFramework.Plus.QueryFilterContext.EnableFilter(BaseQueryFilter filter, Type[] types)
at Z.EntityFramework.Plus.QueryFilterManager.InitilizeGlobalFilter(DbContext context)

@zzzprojects zzzprojects self-assigned this Nov 6, 2016
@zzzprojects
Copy link
Collaborator

zzzprojects commented Nov 6, 2016

Hello @sw-ch ,

Unfortunately, this is a documented limitation for EF Core: EF+ Query Filter - Limitations

This issue is due because EF Core cannot handle all casting scenario: dotnet/efcore#3736

By example

var sum1 = context.Inheritance_Interface_Entities
    .Cast<Inheritance_Interface_Entity>()
    .Sum(x => x.ColumnInt); // Aggregate, Max, Min, Sum

var sum2 = context.Inheritance_Interface_Entities
    .Cast<Inheritance_Interface_IEntity>()
    .Sum(x => x.ColumnInt); // Aggregate, Max, Min, Sum

var sum3 = context.Inheritance_Interface_Entities
    .Cast<Inheritance_Interface_IEntity>()
    .Cast<Inheritance_Interface_Entity>()
    .Sum(x => x.ColumnInt); // Aggregate, Max, Min, Sum

all throws exceptions since (Aggregate, Max, Min, Sum) is not handled correctly when a cast is used.

I will take one hour or two today to see if I can use a workaround since the fix has been reported multiple times (to milestone 7.00 -> 1.00 -> 1.01 -> 1.1.0-preview1 -> 1.2.0) by the EF Team.

Best Regards,

Jonathan

@sw-ch
Copy link
Author

sw-ch commented Nov 6, 2016

Hi Jonathan

Thanks for your quick reply. Sorry for bothering, should have seen that in the documentation.

Regards
Stephan

@zzzprojects
Copy link
Collaborator

Hello Stephan ,

Don't be sorry for that. It's always good to have a reminder from time to time to check if Cast method has been fixed or not.

A 'ForceCast' option has been added which may or not help you with this issue.

QueryFilterManager.ForceCast = true;

When enabled, Inherited class will now work and you will not longer get this error. However, some LINQ method will not longer work with Filtered query:

  • Aggregate
  • Max
  • Min
  • Sum

Let me know if that can partially fix your situation for now.

Best Regards,

Jonathan

@sw-ch
Copy link
Author

sw-ch commented Nov 7, 2016

Hi Jonathan

Thanks for the new version. Currently, I should be able to work around the limitations. It also seems to work with single table-per-hierarchy inheritance entities. I will need some time to figure out how much ground I can cover without hitting another wall...

Should I close the issue for now or do you want to keep it open as a reminder?

Regards
Stephan

@zzzprojects
Copy link
Collaborator

Hello @sw-ch ,

Let close it, that will make it easier to track other issues.

Best Regards,

Jonathan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant