diff --git a/src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.cs b/src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.cs index 1afaed8..a04c566 100644 --- a/src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.cs +++ b/src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.cs @@ -136,11 +136,15 @@ namespace EntityFrameworkCore.Projectables.Generator if (node.Parent is MemberAccessExpressionSyntax parentMemberAccessNode) { var targetSymbolInfo = _semanticModel.GetSymbolInfo(parentMemberAccessNode.Expression); - if (targetSymbolInfo.Symbol is { Kind: SymbolKind.Parameter or SymbolKind.NamedType }) + if (targetSymbolInfo.Symbol is null) { rewrite = false; } - if (targetSymbolInfo.Symbol?.ContainingType is not null) + else if (targetSymbolInfo.Symbol is { Kind: SymbolKind.Parameter or SymbolKind.NamedType }) + { + rewrite = false; + } + else if (targetSymbolInfo.Symbol?.ContainingType is not null) { if (!_compilation.HasImplicitConversion(targetSymbolInfo.Symbol.ContainingType, _targetTypeSymbol) || !SymbolEqualityComparer.Default.Equals(symbolInfo.Symbol.ContainingType, _targetTypeSymbol)) diff --git a/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.Cast.verified.txt b/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.Cast.verified.txt new file mode 100644 index 0000000..c744f49 --- /dev/null +++ b/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.Cast.verified.txt @@ -0,0 +1,15 @@ +using EntityFrameworkCore.Projectables; +using Projectables.Repro; + +namespace EntityFrameworkCore.Projectables.Generated +#nullable disable +{ + public static class Projectables_Repro_SomeExtensions_AsSomeResult + { + public static System.Linq.Expressions.Expression> Expression() + { + return (global::Projectables.Repro.SomeEntity e) => + ((global::Projectables.Repro.SuperEntity)e).Superpower; + } + } +} \ No newline at end of file diff --git a/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.EnumAccessor.received.txt b/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.EnumAccessor.received.txt deleted file mode 100644 index 366603d..0000000 --- a/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.EnumAccessor.received.txt +++ /dev/null @@ -1,14 +0,0 @@ -using EntityFrameworkCore.Projectables; - -namespace EntityFrameworkCore.Projectables.Generated -#nullable disable -{ - public static class _SomeExtensions_Test - { - public static System.Linq.Expressions.Expression> Expression() - { - return (global::SomeFlag f) => - f == global::SomeFlag.Foo; - } - } -} \ No newline at end of file diff --git a/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs b/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs index c6d06bf..b264a71 100644 --- a/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs +++ b/tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs @@ -1045,6 +1045,39 @@ public static class SomeExtensions return Verifier.Verify(result.GeneratedTrees[0].ToString()); } + [Fact] + public Task Cast() + { + var compilation = CreateCompilation(@" +using EntityFrameworkCore.Projectables; + +namespace Projectables.Repro; + +public class SuperEntity : SomeEntity +{ + public string Superpower { get; set; } +} + +public class SomeEntity +{ + public int Id { get; set; } +} + +public static class SomeExtensions +{ + [Projectable] + public static string AsSomeResult(this SomeEntity e) => ((SuperEntity)e).Superpower; +} +"); + + var result = RunGenerator(compilation); + + Assert.Empty(result.Diagnostics); + Assert.Single(result.GeneratedTrees); + + return Verifier.Verify(result.GeneratedTrees[0].ToString()); + } + #region Helpers Compilation CreateCompilation(string source, bool expectedToCompile = true)