Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,14 @@ x is IPropertySymbol xProperty &&
}
else if (memberBody is PropertyDeclarationSyntax propertyDeclarationSyntax)
{
if (propertyDeclarationSyntax.ExpressionBody is null)
var expressionBody = propertyDeclarationSyntax.ExpressionBody;
if (expressionBody is null)
{
//try to get from getter
var getter = propertyDeclarationSyntax.AccessorList?.Accessors.FirstOrDefault(x => x.Kind() == SyntaxKind.GetAccessorDeclaration);
expressionBody = getter?.ExpressionBody;
}
if (expressionBody is null)
{
var diagnostic = Diagnostic.Create(Diagnostics.RequiresExpressionBodyDefinition, propertyDeclarationSyntax.GetLocation(), memberSymbol.Name);
context.ReportDiagnostic(diagnostic);
Expand All @@ -236,7 +243,7 @@ x is IPropertySymbol xProperty &&
var returnType = declarationSyntaxRewriter.Visit(propertyDeclarationSyntax.Type);

descriptor.ReturnTypeName = returnType.ToString();
descriptor.ExpressionBody = (ExpressionSyntax)expressionSyntaxRewriter.Visit(propertyDeclarationSyntax.ExpressionBody.Expression);
descriptor.ExpressionBody = (ExpressionSyntax)expressionSyntaxRewriter.Visit(expressionBody.Expression);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public record Entity

[Projectable]
public int Computed2 => Id * 2;

[Projectable]
public int Alias
{
get => Id;
set => Id = value;
}
}

[Fact]
Expand Down Expand Up @@ -80,5 +87,28 @@ public Task CombineSelectProjectableProperties()

return Verifier.Verify(query.ToQueryString());
}


[Fact]
public Task FilterOnAliasProperty()
{
using var dbContext = new SampleDbContext<Entity>();

var query = dbContext.Set<Entity>()
.Where(x => x.Alias == 1);

return Verifier.Verify(query.ToQueryString());
}

[Fact]
public Task SelectAliasProperty()
{
using var dbContext = new SampleDbContext<Entity>();

var query = dbContext.Set<Entity>()
.Select(x => x.Alias);

return Verifier.Verify(query.ToQueryString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// <auto-generated/>
#nullable disable
using System;
using EntityFrameworkCore.Projectables;
using Foo;

namespace EntityFrameworkCore.Projectables.Generated
{
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
static class Foo_C_Foo
{
static global::System.Linq.Expressions.Expression<global::System.Func<global::Foo.C, int>> Expression()
{
return (global::Foo.C @this) => @this.Bar;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -127,6 +128,34 @@ class C {
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}

[Fact]
public Task SimpleProjectableComputedPropertyWithSetter()
{
var compilation = CreateCompilation(@"
using System;
using EntityFrameworkCore.Projectables;
namespace Foo {
class C {
public int Bar { get; set; }

[Projectable]
public int Foo
{
get => Bar;
set => Bar = value;
}
}
}
");

var result = RunGenerator(compilation);

Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);

return Verifier.Verify(result.GeneratedTrees[0].ToString());
}

[Fact]
public Task SimpleProjectableComputedInNestedClassProperty()
{
Expand Down Expand Up @@ -470,28 +499,6 @@ static class C {
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}

[Fact]
public void BlockBodiedMember_RaisesDiagnostics()
{
var compilation = CreateCompilation(@"
using System;
using EntityFrameworkCore.Projectables;
namespace Foo {
class C {
[Projectable]
public int Foo
{
get => 1;
}
}
}
");

var result = RunGenerator(compilation);

Assert.Single(result.Diagnostics);
}

[Fact]
public void BlockBodiedMethod_RaisesDiagnostics()
{
Expand Down Expand Up @@ -1617,7 +1624,7 @@ public Task GenericTypesWithConstraints()

#region Helpers

Compilation CreateCompilation(string source, bool expectedToCompile = true)
Compilation CreateCompilation([StringSyntax("c#")]string source, bool expectedToCompile = true)
{
var references = Basic.Reference.Assemblies.NetStandard20.All.ToList();
references.Add(MetadataReference.CreateFromFile(typeof(ProjectableAttribute).Assembly.Location));
Expand Down