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 @@ -5,6 +5,6 @@ public class Entity
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ public static class EntityExtensions
[Projectable]
public int Multiply(int factor) => e.Id * factor;
}

extension (GenericWrapper<Entity> w)
{
/// <summary>
/// Extension member method that multiplies the wrapped entity's ID by a factor.
/// </summary>
[Projectable]
public int MultiplyWrapped(int factor) => w.Wrapped.Id * factor;
}
}

public static class IntExtensions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT [e].[Id] * 10
FROM [Entity] AS [e]
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ public Task ExtensionMemberMethodWithParameterOnEntity()

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

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

var query = dbContext.Set<Entity>()
.Select(x => new GenericWrapper<Entity> { Wrapped = x })
.Select(w => w.MultiplyWrapped(10));

return Verifier.Verify(query.ToQueryString());
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#if NET10_0_OR_GREATER
namespace EntityFrameworkCore.Projectables.FunctionalTests.ExtensionMembers
{
public sealed class GenericWrapper<T>
{
public required T Wrapped { get; set; }
}
}
#endif
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_EntityExtensions_TimesHundred
{
static global::System.Linq.Expressions.Expression<global::System.Func<global::Foo.Wrapper<global::Foo.Entity>, int>> Expression()
{
return (global::Foo.Wrapper<global::Foo.Entity> @this) => @this.Wrapped.Value * 100;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public Task ExtensionMemberProperty()
using EntityFrameworkCore.Projectables;
namespace Foo {
class Entity {
class Entity {
public int Id { get; set; }
}
static class EntityExtensions {
extension(Entity e) {
[Projectable]
Expand All @@ -46,10 +46,10 @@ public Task ExtensionMemberMethod()
using EntityFrameworkCore.Projectables;
namespace Foo {
class Entity {
class Entity {
public int Id { get; set; }
}
static class EntityExtensions {
extension(Entity e) {
[Projectable]
Expand All @@ -75,10 +75,10 @@ public Task ExtensionMemberMethodWithParameters()
using EntityFrameworkCore.Projectables;
namespace Foo {
class Entity {
class Entity {
public int Id { get; set; }
}
static class EntityExtensions {
extension(Entity e) {
[Projectable]
Expand Down Expand Up @@ -127,11 +127,11 @@ public Task ExtensionMemberWithMemberAccess()
using EntityFrameworkCore.Projectables;
namespace Foo {
class Entity {
class Entity {
public int Id { get; set; }
public string Name { get; set; }
}
static class EntityExtensions {
extension(Entity e) {
[Projectable]
Expand Down Expand Up @@ -277,7 +277,39 @@ static class EntityExtensions {
Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);

return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public Task ExtensionMemberOnGenericType()
{
var compilation = CreateCompilation(@"
using System;
using EntityFrameworkCore.Projectables;
namespace Foo {
class Entity {
public int Value { get; set; }
}
class Wrapper<T> {
public T Wrapped { get; set; }
}
static class EntityExtensions {
extension(Wrapper<Entity> e) {
[Projectable]
public int TimesHundred => e.Wrapped.Value * 100;
}
}
}
");

var result = RunGenerator(compilation);

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

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