Skip to content

Commit 250e99c

Browse files
committed
Fix to #33073 - JSON columns throws Invalid token type: 'StartObject' exception with AsNoTrackingWithIdentityResolution()
Problem was that we were treating NoTrackingWithIdentityResolution queries as regular NoTracking in the context of JSON queries. However due to how JSON is materialized (streaming + nested includes are part of the parent materialization, rather than each entity materialized separately) we have special provisions when ChangeTracker is being used. In NoTrackingWithIdentityResolution, ChangeTracker is being used but the materializer didn't adjust to that, which lead to errors. Fix is to generate JSON materializer code based on whether query uses Change Tracker rather than if it's a Tracking/NoTracking query. However, this uncovered some issue with NoTrackingWithIdentityResolution - depending on the order in which entities are processed during materialization, they could cause data corruption (wrong order) when materializing JSON collections. Also, using queryable operators on JSON collections may cause errors or data corruption - we don't propagate key values of those queries to the materializer, so those entities end up with null keys. Adding a validator that makes sure that entities are visited in the correct order and issues exception instructing what to do, if the order is wrong. Also we disable usage of queryable operators, due to the issue mentioned above, and cases where parameters are being used to access collection element in the navigatio chain. For cases with parameters, we can't tell if the value is the same or different so can't properly validate those cases. Two different parameters can have the same value, leading to the same entity being materialized, but when we analyze their JSON path, those paths look different. Fixes #33073
1 parent 8173631 commit 250e99c

File tree

8 files changed

+1490
-49
lines changed

8 files changed

+1490
-49
lines changed

src/EFCore.Relational/Properties/RelationalStrings.Designer.cs

+24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/EFCore.Relational/Properties/RelationalStrings.resx

+9
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,15 @@
559559
<data name="JsonNodeMustBeHandledByProviderSpecificVisitor" xml:space="preserve">
560560
<value>This node should be handled by provider-specific sql generator.</value>
561561
</data>
562+
<data name="JsonProjectingCollectionElementAccessedUsingParmeterNoTrackingWithIdentityResolution" xml:space="preserve">
563+
<value>Using parameter to access the element of a JSON collection '{entityTypeName}' is not supported for '{asNoTrackingWithIdentityResolution}'. Use constant, or project the entire JSON entity collection instead.</value>
564+
</data>
565+
<data name="JsonProjectingEntitiesIncorrectOrderNoTrackingWithIdentityResolution" xml:space="preserve">
566+
<value>When using '{asNoTrackingWithIdentityResolution}' entities mapped to JSON must be projected in a particular order. Project entire collection of entities '{entityTypeName}' before its individual elements.</value>
567+
</data>
568+
<data name="JsonProjectingQueryableOperationNoTrackingWithIdentityResolution" xml:space="preserve">
569+
<value>Projecting queryable operations on JSON collection is not supported for '{asNoTrackingWithIdentityResolution}'.</value>
570+
</data>
562571
<data name="JsonPropertyNameShouldBeConfiguredOnNestedNavigation" xml:space="preserve">
563572
<value>The JSON property name should only be configured on nested owned navigations.</value>
564573
</data>

src/EFCore.Relational/Query/RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.cs

+253-7
Large diffs are not rendered by default.

src/EFCore/Query/QueryContext.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ public virtual void InitializeStateManager(bool standAlone = false)
125125
/// </summary>
126126
[EntityFrameworkInternal]
127127
public virtual InternalEntityEntry? TryGetEntry(
128-
IKey key,
129-
object[] keyValues,
130-
bool throwOnNullKey,
131-
out bool hasNullKey)
128+
IKey key,
129+
object[] keyValues,
130+
bool throwOnNullKey,
131+
out bool hasNullKey)
132132
// InitializeStateManager will populate the field before calling here
133133
=> _stateManager!.TryGetEntry(key, keyValues, throwOnNullKey, out hasNullKey);
134134

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

+904-7
Large diffs are not rendered by default.

test/EFCore.Specification.Tests/Query/AdHocMiscellaneousQueryTestBase.cs

+12-12
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,20 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
160160

161161
public DbSet<Customer> Customers { get; set; }
162162
public DbSet<Postcode> Postcodes { get; set; }
163-
}
164163

165-
public class Customer
166-
{
167-
public int CustomerID { get; set; }
168-
public string CustomerName { get; set; }
169-
public int? PostcodeID { get; set; }
170-
}
164+
public class Customer
165+
{
166+
public int CustomerID { get; set; }
167+
public string CustomerName { get; set; }
168+
public int? PostcodeID { get; set; }
169+
}
171170

172-
public class Postcode
173-
{
174-
public int PostcodeID { get; set; }
175-
public string PostcodeValue { get; set; }
176-
public string TownName { get; set; }
171+
public class Postcode
172+
{
173+
public int PostcodeID { get; set; }
174+
public string PostcodeValue { get; set; }
175+
public string TownName { get; set; }
176+
}
177177
}
178178

179179
#endregion

0 commit comments

Comments
 (0)