Skip to content

Commit 7ee6e83

Browse files
committed
Fix to #28595 - Use partial updates for JSON
Currently, whenever we update/add/delete part of aggregate mapped to JSON column, we were replacing the entire structure. Improvement is to use JSON_MODIFY which can alter just a portion of JSON structure, give a JSON path. We analyze the entries that are being edited and for each JSON column we find the common denominator that needs to be replaced to accommodate all the requested changes Note: Whenever we add/remove element from a collection we need to replace the entire collection in order to populate ordinal key values properly. Also, modifying a single property is not yet supported - the smallest fragment that will be replaced is one that represents an entity. Fixes #28595
1 parent c57ecee commit 7ee6e83

File tree

11 files changed

+933
-149
lines changed

11 files changed

+933
-149
lines changed

src/EFCore.Relational/Update/ColumnModification.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Data;
5-
64
namespace Microsoft.EntityFrameworkCore.Update;
75

86
/// <summary>
@@ -54,6 +52,7 @@ public ColumnModification(in ColumnModificationParameters columnModificationPara
5452
IsNullable = columnModificationParameters.IsNullable;
5553
_generateParameterName = columnModificationParameters.GenerateParameterName;
5654
Entry = columnModificationParameters.Entry;
55+
JsonPath = columnModificationParameters.JsonPath;
5756

5857
UseParameter = _generateParameterName != null;
5958
}
@@ -174,6 +173,9 @@ public virtual object? Value
174173
}
175174
}
176175

176+
/// <inheritdoc />
177+
public virtual string? JsonPath { get; }
178+
177179
/// <inheritdoc />
178180
public virtual void AddSharedColumnModification(IColumnModification modification)
179181
{

src/EFCore.Relational/Update/ColumnModificationParameters.cs

+58-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public readonly record struct ColumnModificationParameters
7676
/// Indicates whether the column is part of a primary or alternate key.
7777
/// </summary>
7878
public bool IsKey { get; init; }
79-
79+
8080
/// <summary>
8181
/// The column.
8282
/// </summary>
@@ -92,6 +92,11 @@ public readonly record struct ColumnModificationParameters
9292
/// </summary>
9393
public string? ColumnType { get; init; }
9494

95+
/// <summary>
96+
/// In case of JSON column modification, the JSON path leading to the JSON element that needs to be updated.
97+
/// </summary>
98+
public string? JsonPath { get; init; }
99+
95100
/// <summary>
96101
/// Creates a new <see cref="ColumnModificationParameters" /> instance.
97102
/// </summary>
@@ -137,8 +142,9 @@ public ColumnModificationParameters(
137142

138143
GenerateParameterName = null;
139144
Entry = null;
145+
JsonPath = null;
140146
}
141-
147+
142148
/// <summary>
143149
/// Creates a new <see cref="ColumnModificationParameters" /> instance.
144150
/// </summary>
@@ -182,6 +188,7 @@ public ColumnModificationParameters(
182188

183189
GenerateParameterName = null;
184190
Entry = null;
191+
JsonPath = null;
185192
}
186193

187194
/// <summary>
@@ -225,5 +232,54 @@ public ColumnModificationParameters(
225232

226233
GenerateParameterName = generateParameterName;
227234
Entry = entry;
235+
JsonPath = null;
236+
}
237+
238+
/// <summary>
239+
/// Creates a new <see cref="ColumnModificationParameters" /> instance.
240+
/// </summary>
241+
/// <param name="columnName">The name of the column.</param>
242+
/// <param name="originalValue">The original value of the property mapped to this column.</param>
243+
/// <param name="value">The current value of the property mapped to this column.</param>
244+
/// <param name="jsonPath">The JSON path leading to the JSON element that needs to be updated.</param>
245+
/// <param name="columnType">The database type of the column.</param>
246+
/// <param name="typeMapping">The relational type mapping to be used for the command parameter.</param>
247+
/// <param name="read">Indicates whether a value must be read from the database for the column.</param>
248+
/// <param name="write">Indicates whether a value must be written to the database for the column.</param>
249+
/// <param name="key">Indicates whether the column part of a primary or alternate key.</param>
250+
/// <param name="condition">Indicates whether the column is used in the <c>WHERE</c> clause when updating.</param>
251+
/// <param name="sensitiveLoggingEnabled">Indicates whether potentially sensitive data (e.g. database values) can be logged.</param>
252+
/// <param name="isNullable">A value indicating whether the value could be null.</param>
253+
public ColumnModificationParameters(
254+
string columnName,
255+
object? originalValue,
256+
object? value,
257+
string? columnType,
258+
RelationalTypeMapping? typeMapping,
259+
string jsonPath,
260+
bool read,
261+
bool write,
262+
bool key,
263+
bool condition,
264+
bool sensitiveLoggingEnabled,
265+
bool? isNullable = null)
266+
{
267+
Column = null;
268+
ColumnName = columnName;
269+
OriginalValue = originalValue;
270+
Value = value;
271+
Property = null;
272+
ColumnType = columnType;
273+
TypeMapping = typeMapping;
274+
JsonPath = jsonPath;
275+
IsRead = read;
276+
IsWrite = write;
277+
IsKey = key;
278+
IsCondition = condition;
279+
SensitiveLoggingEnabled = sensitiveLoggingEnabled;
280+
IsNullable = isNullable;
281+
282+
GenerateParameterName = null;
283+
Entry = null;
228284
}
229285
}

src/EFCore.Relational/Update/IColumnModification.cs

+5
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ public interface IColumnModification
122122
/// </summary>
123123
public object? Value { get; set; }
124124

125+
/// <summary>
126+
/// In case of JSON column modification, the JSON path leading to the JSON element that needs to be updated.
127+
/// </summary>
128+
public string? JsonPath { get; }
129+
125130
/// <summary>
126131
/// Adds a modification affecting the same database value.
127132
/// </summary>

0 commit comments

Comments
 (0)