Skip to content

Comments

Add repro test and fix for abstract method override calling base property in polymorphic base query#314

Open
mikevmil wants to merge 3 commits intohazzik:mainfrom
mikevmil:main
Open

Add repro test and fix for abstract method override calling base property in polymorphic base query#314
mikevmil wants to merge 3 commits intohazzik:mainfrom
mikevmil:main

Conversation

@mikevmil
Copy link

Hi hazzik,

This PR adds a minimal repro test for a bug I’m hitting when using DelegateDecompiler with EF in a polymorphic query scenario.

What the test covers

  • There is an abstract base class with a base property.
  • The base class declares an abstract method.
  • A derived/concrete class overrides that abstract method.
  • The override reads the base property.
  • The query is executed from the base set/type (i.e. we keep the base type in the query, not using OfType() towards the actual concrete type / explicit casting).

In this setup, projecting the abstract method (implemented in the derived type) causes the query translation/decompilation to fail as I get the following error:

Message: 
System.NotSupportedException : Unable to cast the type 'DelegateDecompiler.EntityFramework.Tests.EfItems.Abstracts.Animal' to type 'DelegateDecompiler.EntityFramework.Tests.EfItems.Abstracts.HoneyBee'. LINQ to Entities only supports casting EDM primitive or enumeration types.

Stack Trace: 
ExpressionConverter.ValidateAndAdjustCastTypes(TypeUsage toType, TypeUsage fromType, Type toClrType, Type fromClrType)
ExpressionConverter.GetCastTargetType(TypeUsage fromType, Type toClrType, Type fromClrType, Boolean preserveCastForDateTime)
ExpressionConverter.CreateCastExpression(DbExpression source, Type toClrType, Type fromClrType)
ExpressionConverter.TranslateExpression(Expression linq)
MemberAccessTranslator.TypedTranslate(ExpressionConverter parent, MemberExpression linq)
ExpressionConverter.TranslateExpression(Expression linq)
StringTranslatorUtil.ConvertToString(ExpressionConverter parent, Expression linqExpression)
WhereSelectArrayIterator`2.MoveNext()
Buffer`1.ctor(IEnumerable`1 source)
Enumerable.ToArray[TSource](IEnumerable`1 source)
<11 more frames...>
<>c__DisplayClass41_0.<GetResults>b__1()
ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
<>c__DisplayClass41_0.<GetResults>b__0()
DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
IEnumerable<T>.GetEnumerator>b__31_0()
LazyEnumerator`1.MoveNext()
List`1.ctor(IEnumerable`1 collection)
Enumerable.ToList[TSource](IEnumerable`1 source)
Test01Select.TestSelectAbstractMethodImplementedInDerivedCallingBasePropertyFromBaseType() line 246

When I look deeper, it seems like it tries to cast LivingBeein/Animal to Dog/HoneyBee when retrieving the value of the base class, which fails within Entity Framework and shouldn't even be needed considering (in my case) I'm using TPH.

I’m opening this PR primarily to contribute a repeatable, automated repro that documents the exact scenario and prevent possible regressions once a fix is found.

If someone familiar with the decompiler/EF translation path can point me at where this cast is being introduced, or provide a fix, I’d really appreciate the help/guidance.

Mike van Mil added 3 commits February 19, 2026 17:27
Introduce AlignInstanceToDeclaringType to ensure the instance used in method and property calls matches the method's declaring type.
@mikevmil
Copy link
Author

I've added code that removes the unnecessary cast, thus the test that I've written is now passing.

@mikevmil mikevmil changed the title Add repro test for abstract method override calling base property in polymorphic base query Add repro test and fix for abstract method override calling base property in polymorphic base query Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant