mirror of
https://github.com/zoriya/EntityFrameworkCore.Projectables.git
synced 2025-12-06 05:56:10 +00:00
Merge pull request #11 from koenbeuk/issue-10
Support Inherited properties
This commit is contained in:
@@ -98,7 +98,7 @@ namespace EntityFrameworkCore.Projectables.Generator
|
||||
{
|
||||
var symbolInfo = _semanticModel.GetSymbolInfo(node);
|
||||
|
||||
if (symbolInfo.Symbol is not null && SymbolEqualityComparer.Default.Equals(symbolInfo.Symbol.ContainingType, _targetTypeSymbol))
|
||||
if (symbolInfo.Symbol is not null)
|
||||
{
|
||||
var scopedNode = node.ChildNodes().FirstOrDefault();
|
||||
if (scopedNode is ThisExpressionSyntax)
|
||||
@@ -127,7 +127,7 @@ namespace EntityFrameworkCore.Projectables.Generator
|
||||
if (symbolInfo.Symbol is IMethodSymbol methodSymbol && methodSymbol.IsExtensionMethod)
|
||||
{
|
||||
}
|
||||
else if (symbolInfo.Symbol.Kind is SymbolKind.Property or SymbolKind.Method or SymbolKind.Field && SymbolEqualityComparer.Default.Equals(symbolInfo.Symbol.ContainingType, _targetTypeSymbol))
|
||||
else if (symbolInfo.Symbol.Kind is SymbolKind.Property or SymbolKind.Method or SymbolKind.Field /*&& SymbolEqualityComparer.Default.Equals(symbolInfo.Symbol.ContainingType, _targetTypeSymbol)*/)
|
||||
{
|
||||
bool rewrite = true;
|
||||
|
||||
@@ -138,6 +138,10 @@ namespace EntityFrameworkCore.Projectables.Generator
|
||||
{
|
||||
rewrite = false;
|
||||
}
|
||||
if (targetSymbolInfo.Symbol?.ContainingType is not null && !_context.Compilation.HasImplicitConversion(targetSymbolInfo.Symbol.ContainingType, _targetTypeSymbol))
|
||||
{
|
||||
rewrite = false;
|
||||
}
|
||||
}
|
||||
else if (node.Parent.IsKind(SyntaxKind.SimpleAssignmentExpression))
|
||||
{
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
SELECT CAST([c].[Age] AS float) / [c].[AverageLifespan] AS [LifeProgression], CAST([c].[MentalAge] AS float) / [c].[AverageLifespan] AS [MentalLifeProgression]
|
||||
FROM [Cat] AS [c]
|
||||
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using EntityFrameworkCore.Projectables.FunctionalTests.Helpers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using VerifyXunit;
|
||||
using Xunit;
|
||||
|
||||
namespace EntityFrameworkCore.Projectables.FunctionalTests
|
||||
{
|
||||
[UsesVerify]
|
||||
public class InheritedMembersTests
|
||||
{
|
||||
public abstract class Animal
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public double AverageLifespan { get; set; }
|
||||
public int Age { get; set; }
|
||||
|
||||
[Projectable]
|
||||
public double LifeProgression
|
||||
=> Age / AverageLifespan;
|
||||
}
|
||||
|
||||
public class Cat : Animal
|
||||
{
|
||||
public int MentalAge { get; set; }
|
||||
|
||||
[Projectable]
|
||||
public double MentalLifeProgression
|
||||
=> MentalAge / AverageLifespan;
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public Task ProjectOverMethodTakingDbContext()
|
||||
{
|
||||
using var dbContext = new SampleDbContext<Cat>();
|
||||
|
||||
var query = dbContext.Set<Cat>()
|
||||
.Select(x => new { x.LifeProgression, x.MentalLifeProgression });
|
||||
|
||||
return Verifier.Verify(query.ToQueryString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,11 +23,6 @@
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="**\*.verified.txt" />
|
||||
<None Remove="**\*.received.txt" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\EntityFrameworkCore.Projectables.Generator\EntityFrameworkCore.Projectables.Generator.csproj" />
|
||||
<ProjectReference Include="..\..\src\EntityFrameworkCore.Projectables\EntityFrameworkCore.Projectables.csproj" />
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using EntityFrameworkCore.Projectables;
|
||||
using Foo;
|
||||
|
||||
namespace EntityFrameworkCore.Projectables.Generated
|
||||
#nullable disable
|
||||
{
|
||||
public static class Foo_Bar_ProjectedId
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<global::Foo.Bar, int>> Expression()
|
||||
{
|
||||
return (global::Foo.Bar @this) =>
|
||||
@this.Id;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -959,6 +959,35 @@ namespace Foo {
|
||||
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task InheritedMembers()
|
||||
{
|
||||
var compilation = CreateCompilation(@"
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using EntityFrameworkCore.Projectables;
|
||||
|
||||
namespace Foo {
|
||||
public class Foo {
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class Bar : Foo {
|
||||
[Projectable]
|
||||
public int ProjectedId => Id;
|
||||
}
|
||||
}
|
||||
");
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user