Skip to content

Commit 4341961

Browse files
authored
Merge pull request #33200 from dotnet/moar_tests_32911
Additional test for #32911 plus a small code correction
2 parents 51f7aa2 + 74ad389 commit 4341961

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2166,7 +2166,7 @@ StructuralTypeProjectionExpression ProcessStructuralType(
21662166
structuralProjection1.TableMap.Keys.All(t => structuralProjection2.TableMap.ContainsKey(t)),
21672167
"Set operation over entity projections with table map discrepancy");
21682168

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

21712171
var discriminatorExpression = structuralProjection1.DiscriminatorExpression;
21722172
if (structuralProjection1.DiscriminatorExpression != null

test/EFCore.Relational.Specification.Tests/Query/AdHocAdvancedMappingsQueryRelationalTestBase.cs

+83
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ public virtual async Task Two_similar_complex_properties_projected_with_split_qu
5555
}
5656
}
5757

58+
[ConditionalFact]
59+
public virtual async Task Projecting_one_of_two_similar_complex_types_picks_the_correct_one()
60+
{
61+
var contextFactory = await InitializeAsync<Context32911_2>(seed: c => c.Seed());
62+
63+
using var context = contextFactory.CreateContext();
64+
65+
var query = context.Cs
66+
.Where(x => x.B.AId.Value == 1)
67+
.OrderBy(x => x.Id)
68+
.Take(10)
69+
.Select(x => new
70+
{
71+
x.B.A.Id,
72+
x.B.Info.Created,
73+
}).ToList();
74+
75+
Assert.Equal(new DateTime(2000, 1, 1), query[0].Created);
76+
}
77+
5878
protected class Context32911(DbContextOptions options) : DbContext(options)
5979
{
6080
public DbSet<Offer> Offers { get; set; }
@@ -143,6 +163,69 @@ public class NestedEntity : EntityBase
143163
public record Payment(decimal Netto, decimal Brutto);
144164
}
145165

166+
protected class Context32911_2(DbContextOptions options) : DbContext(options)
167+
{
168+
public DbSet<A> As { get; set; }
169+
public DbSet<B> Bs { get; set; }
170+
public DbSet<C> Cs { get; set; }
171+
172+
protected override void OnModelCreating(ModelBuilder modelBuilder)
173+
{
174+
modelBuilder.Entity<A>().Property(x => x.Id).ValueGeneratedNever();
175+
modelBuilder.Entity<B>().Property(x => x.Id).ValueGeneratedNever();
176+
modelBuilder.Entity<C>().Property(x => x.Id).ValueGeneratedNever();
177+
178+
modelBuilder.Entity<B>(x => x.ComplexProperty(b => b.Info).IsRequired());
179+
modelBuilder.Entity<C>(x => x.ComplexProperty(c => c.Info).IsRequired());
180+
}
181+
182+
public void Seed()
183+
{
184+
var c = new C
185+
{
186+
Id = 100,
187+
Info = new Metadata { Created = new DateTime(2020, 10, 10) },
188+
B = new B
189+
{
190+
Id = 10,
191+
Info = new Metadata { Created = new DateTime(2000, 1, 1) },
192+
A = new A { Id = 1 }
193+
}
194+
};
195+
196+
Cs.Add(c);
197+
SaveChanges();
198+
}
199+
200+
public class Metadata
201+
{
202+
public DateTime Created { get; set; }
203+
}
204+
205+
public class A
206+
{
207+
public int Id { get; set; }
208+
}
209+
210+
public class B
211+
{
212+
public int Id { get; set; }
213+
public Metadata Info { get; set; }
214+
public int? AId { get; set; }
215+
216+
public A A { get; set; }
217+
}
218+
219+
public class C
220+
{
221+
public int Id { get; set; }
222+
public Metadata Info { get; set; }
223+
public int BId { get; set; }
224+
225+
public B B { get; set; }
226+
}
227+
}
228+
146229
#endregion
147230

148231
[ConditionalTheory]

test/EFCore.SqlServer.FunctionalTests/Query/AdHocAdvancedMappingsQuerySqlServerTest.cs

+21
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,27 @@ FROM [Variation] AS [v]
282282
LEFT JOIN [NestedEntity] AS [n] ON [v].[NestedId] = [n].[Id]
283283
) AS [s] ON [o0].[Id] = [s].[OfferId]
284284
ORDER BY [o0].[Id]
285+
""");
286+
}
287+
288+
public override async Task Projecting_one_of_two_similar_complex_types_picks_the_correct_one()
289+
{
290+
await base.Projecting_one_of_two_similar_complex_types_picks_the_correct_one();
291+
292+
AssertSql(
293+
"""
294+
@__p_0='10'
295+
296+
SELECT [a].[Id], [s].[Info_Created0] AS [Created]
297+
FROM (
298+
SELECT TOP(@__p_0) [c].[Id], [b].[AId], [b].[Info_Created] AS [Info_Created0]
299+
FROM [Cs] AS [c]
300+
INNER JOIN [Bs] AS [b] ON [c].[BId] = [b].[Id]
301+
WHERE [b].[AId] = 1
302+
ORDER BY [c].[Id]
303+
) AS [s]
304+
LEFT JOIN [As] AS [a] ON [s].[AId] = [a].[Id]
305+
ORDER BY [s].[Id]
285306
""");
286307
}
287308
}

0 commit comments

Comments
 (0)