Skip to content

Commit

Permalink
Additional test for #32911 plus a small code correction
Browse files Browse the repository at this point in the history
  • Loading branch information
maumar committed Feb 29, 2024
1 parent e9b942b commit 74ad389
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2166,7 +2166,7 @@ public void ApplyUnion(SelectExpression source2, bool distinct)
structuralProjection1.TableMap.Keys.All(t => structuralProjection2.TableMap.ContainsKey(t)),
"Set operation over entity projections with table map discrepancy");

var tableMap = projection1.TableMap.ToDictionary(kvp => kvp.Key, _ => setOperationAlias);
var tableMap = structuralProjection1.TableMap.ToDictionary(kvp => kvp.Key, _ => setOperationAlias);

var discriminatorExpression = structuralProjection1.DiscriminatorExpression;
if (structuralProjection1.DiscriminatorExpression != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ public virtual async Task Two_similar_complex_properties_projected_with_split_qu
}
}

[ConditionalFact]
public virtual async Task Projecting_one_of_two_similar_complex_types_picks_the_correct_one()
{
var contextFactory = await InitializeAsync<Context32911_2>(seed: c => c.Seed());

using var context = contextFactory.CreateContext();

var query = context.Cs
.Where(x => x.B.AId.Value == 1)
.OrderBy(x => x.Id)
.Take(10)
.Select(x => new
{
x.B.A.Id,
x.B.Info.Created,
}).ToList();

Assert.Equal(new DateTime(2000, 1, 1), query[0].Created);
}

protected class Context32911(DbContextOptions options) : DbContext(options)
{
public DbSet<Offer> Offers { get; set; }
Expand Down Expand Up @@ -143,6 +163,69 @@ public class NestedEntity : EntityBase
public record Payment(decimal Netto, decimal Brutto);
}

protected class Context32911_2(DbContextOptions options) : DbContext(options)
{
public DbSet<A> As { get; set; }
public DbSet<B> Bs { get; set; }
public DbSet<C> Cs { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<A>().Property(x => x.Id).ValueGeneratedNever();
modelBuilder.Entity<B>().Property(x => x.Id).ValueGeneratedNever();
modelBuilder.Entity<C>().Property(x => x.Id).ValueGeneratedNever();

modelBuilder.Entity<B>(x => x.ComplexProperty(b => b.Info).IsRequired());
modelBuilder.Entity<C>(x => x.ComplexProperty(c => c.Info).IsRequired());
}

public void Seed()
{
var c = new C
{
Id = 100,
Info = new Metadata { Created = new DateTime(2020, 10, 10) },
B = new B
{
Id = 10,
Info = new Metadata { Created = new DateTime(2000, 1, 1) },
A = new A { Id = 1 }
}
};

Cs.Add(c);
SaveChanges();
}

public class Metadata
{
public DateTime Created { get; set; }
}

public class A
{
public int Id { get; set; }
}

public class B
{
public int Id { get; set; }
public Metadata Info { get; set; }
public int? AId { get; set; }

public A A { get; set; }
}

public class C
{
public int Id { get; set; }
public Metadata Info { get; set; }
public int BId { get; set; }

public B B { get; set; }
}
}

#endregion

[ConditionalTheory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,27 @@ public override async Task Two_similar_complex_properties_projected_with_split_q
LEFT JOIN [NestedEntity] AS [n] ON [v].[NestedId] = [n].[Id]
) AS [s] ON [o0].[Id] = [s].[OfferId]
ORDER BY [o0].[Id]
""");
}

public override async Task Projecting_one_of_two_similar_complex_types_picks_the_correct_one()
{
await base.Projecting_one_of_two_similar_complex_types_picks_the_correct_one();

AssertSql(
"""
@__p_0='10'

SELECT [a].[Id], [s].[Info_Created0] AS [Created]
FROM (
SELECT TOP(@__p_0) [c].[Id], [b].[AId], [b].[Info_Created] AS [Info_Created0]
FROM [Cs] AS [c]
INNER JOIN [Bs] AS [b] ON [c].[BId] = [b].[Id]
WHERE [b].[AId] = 1
ORDER BY [c].[Id]
) AS [s]
LEFT JOIN [As] AS [a] ON [s].[AId] = [a].[Id]
ORDER BY [s].[Id]
""");
}
}

0 comments on commit 74ad389

Please sign in to comment.