From 51e6e0ed6b6b305f4ba6f280fac29cd4274d8857 Mon Sep 17 00:00:00 2001 From: Spotlight-Zachary <83731064+Spotlight-Zachary@users.noreply.github.com> Date: Thu, 6 Jan 2022 13:54:36 +1300 Subject: [PATCH] Treat the Identity Field as a Duplicated Field (#2016) Treat the Identity Field as a Duplicated Field * Add unit test * Treat IdField as DuplicatedField in SimpleEqualsParser --- .../Bug_id_field_does_not_hit_id_column.cs | 38 +++++++++++++++++++ .../SimpleBinaryComparisonExpressionParser.cs | 2 +- src/Marten/Linq/Parsing/SimpleEqualsParser.cs | 2 +- 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/Marten.Testing/Linq/Bug_id_field_does_not_hit_id_column.cs diff --git a/src/Marten.Testing/Linq/Bug_id_field_does_not_hit_id_column.cs b/src/Marten.Testing/Linq/Bug_id_field_does_not_hit_id_column.cs new file mode 100644 index 0000000000..366ea87165 --- /dev/null +++ b/src/Marten.Testing/Linq/Bug_id_field_does_not_hit_id_column.cs @@ -0,0 +1,38 @@ +using System.Linq; +using Marten.Linq; +using Marten.Services; +using Marten.Testing.Documents; +using Marten.Testing.Harness; +using Shouldly; +using Xunit; + +namespace Marten.Testing.Linq +{ + [SelectionStoryteller] + public class Bug_id_field_does_not_hit_id_column: IntegrationContextWithIdentityMap + { + [Fact] + public void return_the_correct_number_of_results() + { + var target = new Target + { + Id = System.Guid.NewGuid() + }; + + theStore.BulkInsert(new[] { target }); + + var queryable = theSession.Query() + .Where(x => x.Id == target.Id); + + var cmd = queryable.ToCommand(FetchType.FetchMany); + + SpecificationExtensions.ShouldContain(cmd.CommandText, "where d.id = :arg0"); + + queryable.ToArray().Length.ShouldBe(1); + } + + public Bug_id_field_does_not_hit_id_column(DefaultStoreFixture fixture) : base(fixture) + { + } + } +} diff --git a/src/Marten/Linq/Parsing/SimpleBinaryComparisonExpressionParser.cs b/src/Marten/Linq/Parsing/SimpleBinaryComparisonExpressionParser.cs index 8a18c00988..711e8b1b13 100644 --- a/src/Marten/Linq/Parsing/SimpleBinaryComparisonExpressionParser.cs +++ b/src/Marten/Linq/Parsing/SimpleBinaryComparisonExpressionParser.cs @@ -63,7 +63,7 @@ public IWhereFragment Parse(IQueryableDocument mapping, ISerializer serializer, var useContainment = mapping.PropertySearching == PropertySearching.ContainmentOperator || field.ShouldUseContainmentOperator(); - var isDuplicated = (mapping.FieldFor(members) is DuplicatedField); + var isDuplicated = field is DuplicatedField || field is IdField; var isEnumString = field.MemberType.GetTypeInfo().IsEnum && serializer.EnumStorage == EnumStorage.AsString; if (useContainment && diff --git a/src/Marten/Linq/Parsing/SimpleEqualsParser.cs b/src/Marten/Linq/Parsing/SimpleEqualsParser.cs index b02eb7380c..aeb0d68508 100644 --- a/src/Marten/Linq/Parsing/SimpleEqualsParser.cs +++ b/src/Marten/Linq/Parsing/SimpleEqualsParser.cs @@ -72,7 +72,7 @@ public IWhereFragment Parse(IQueryableDocument mapping, ISerializer serializer, if (_supportContainment && ((mapping.PropertySearching == PropertySearching.ContainmentOperator || field.ShouldUseContainmentOperator()) && - !(field is DuplicatedField))) + !(field is DuplicatedField || field is IdField))) { var dict = new Dictionary(); ContainmentWhereFragment.CreateDictionaryForSearch(dict, expression, valueToQuery, serializer);