Skip to content

Commit c04c26d

Browse files
authored
Fix entity equality contains with ParameterizedCollectionMode.Parameter (#36311) (#36340)
1 parent 9957d42 commit c04c26d

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

src/EFCore.Relational/Query/SqlNullabilityProcessor.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,8 @@ ParameterizedCollectionMode is ParameterizedCollectionMode.Constants
830830
||
831831
(ParameterizedCollectionMode is ParameterizedCollectionMode.MultipleParameters
832832
&& valuesParameter.ShouldBeConstantized);
833+
var useParameter = ParameterizedCollectionMode is ParameterizedCollectionMode.Parameter
834+
&& !valuesParameter.ShouldBeConstantized;
833835
var expandedParameters = _collectionParameterExpansionMap.GetOrAddNew(valuesParameter);
834836
var expandedParametersCounter = 0;
835837
for (var i = 0; i < values.Count; i++)
@@ -840,9 +842,11 @@ ParameterizedCollectionMode is ParameterizedCollectionMode.Constants
840842
continue;
841843
}
842844

843-
switch (useParameters, useConstants)
845+
switch (useParameters, useConstants, useParameter)
844846
{
845-
case (true, false):
847+
case (true, false, false):
848+
// see #36311 for more info
849+
case (false, false, true):
846850
{
847851
// Create parameter for value if we didn't create it yet,
848852
// otherwise reuse it.
@@ -860,7 +864,7 @@ ParameterizedCollectionMode is ParameterizedCollectionMode.Constants
860864
break;
861865
}
862866

863-
case (false, true):
867+
case (false, true, false):
864868
{
865869
processedValues.Add(_sqlExpressionFactory.Constant(values[i], values[i]?.GetType() ?? typeof(object), sensitive: true, elementTypeMapping));
866870

@@ -869,7 +873,7 @@ ParameterizedCollectionMode is ParameterizedCollectionMode.Constants
869873

870874
default:
871875
throw new UnreachableException();
872-
};
876+
}
873877
}
874878
}
875879
else

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,45 @@ public class TestEntity
316316
public static readonly IEnumerable<object[]> InlinedRedactingData = [[true, true], [true, false], [false, true], [false, false]];
317317

318318
#endregion
319+
320+
#region 36311
321+
322+
[ConditionalTheory]
323+
[MemberData(nameof(IsAsyncData))]
324+
public async Task Entity_equality_with_Contains_and_Parameter(bool async)
325+
{
326+
var contextFactory = await InitializeAsync<Context36311>(
327+
onConfiguring: o => SetParameterizedCollectionMode(o, ParameterizedCollectionMode.Parameter));
328+
using var context = contextFactory.CreateContext();
329+
330+
List<Context36311.BlogDetails> details = [new Context36311.BlogDetails { Id = 1 }, new Context36311.BlogDetails { Id = 2 }];
331+
var query = context.Blogs.Where(b => details.Contains(b.Details));
332+
333+
var result = async
334+
? await query.ToListAsync()
335+
: query.ToList();
336+
}
337+
338+
protected class Context36311(DbContextOptions options) : DbContext(options)
339+
{
340+
public DbSet<Blog> Blogs { get; set; }
341+
342+
public class Blog
343+
{
344+
public int Id { get; set; }
345+
public string Name { get; set; }
346+
347+
public BlogDetails Details { get; set; }
348+
}
349+
350+
public class BlogDetails
351+
{
352+
public int Id { get; set; }
353+
public string Name { get; set; }
354+
}
355+
}
356+
357+
#endregion
319358
}
320359
}
321360

0 commit comments

Comments
 (0)