Skip to content

Commit efb4811

Browse files
jeremy-morrenclaude
andcommitted
Make default JSON column names unique against other columns
The default name of a JSON complex property nested in a table-split complex property can be the name of another column in the table or view. Such names are made unique, and an explicit JSON column name used by another column is a validation error. Partially addresses #39730 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 24a485d commit efb4811

15 files changed

Lines changed: 765 additions & 35 deletions

File tree

‎src/EFCore.Relational/EFCore.Relational.baseline.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15309,6 +15309,9 @@
1530915309
{
1531015310
"Member": "static string ComplexPropertyInJsonOwnedCollection(object? complexProperty, object? entityType);"
1531115311
},
15312+
{
15313+
"Member": "static string ComplexPropertyJsonColumnUsedByProperty(object? complexProperty, object? column, object? table, object? property);"
15314+
},
1531215315
{
1531315316
"Member": "static string ComplexPropertyJsonPropertyNameConflict(object? property1, object? property2, object? jsonPropertyName);"
1531415317
},

‎src/EFCore.Relational/Extensions/RelationalComplexCollectionBuilderExtensions.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public static class RelationalComplexCollectionBuilderExtensions
2828
/// If the complex collection is declared on a complex type that isn't mapped to JSON and the column name is not specified,
2929
/// the name is prefixed with the names of the containing complex properties. For example, a complex collection
3030
/// <c>Parcels</c> on the complex property <c>Order.Shipping</c> is mapped to the <c>Shipping_Parcels</c> column.
31+
/// A number is appended to the name if it's already used by another column.
3132
/// </para>
3233
/// </remarks>
3334
public static ComplexCollectionBuilder ToJson(
@@ -61,6 +62,7 @@ public static ComplexCollectionBuilder ToJson(
6162
/// If the complex collection is declared on a complex type that isn't mapped to JSON and the column name is not specified,
6263
/// the name is prefixed with the names of the containing complex properties. For example, a complex collection
6364
/// <c>Parcels</c> on the complex property <c>Order.Shipping</c> is mapped to the <c>Shipping_Parcels</c> column.
65+
/// A number is appended to the name if it's already used by another column.
6466
/// </para>
6567
/// </remarks>
6668
public static ComplexCollectionBuilder<TComplex> ToJson<TComplex>(

‎src/EFCore.Relational/Extensions/RelationalComplexPropertyBuilderExtensions.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public static class RelationalComplexPropertyBuilderExtensions
2828
/// If the complex property is declared on a complex type that isn't mapped to JSON and the column name is not specified,
2929
/// the name is prefixed with the names of the containing complex properties. For example, a complex property
3030
/// <c>Address</c> on the complex property <c>Order.Shipping</c> is mapped to the <c>Shipping_Address</c> column.
31+
/// A number is appended to the name if it's already used by another column.
3132
/// </para>
3233
/// </remarks>
3334
public static ComplexPropertyBuilder ToJson(
@@ -61,6 +62,7 @@ public static ComplexPropertyBuilder ToJson(
6162
/// If the complex property is declared on a complex type that isn't mapped to JSON and the column name is not specified,
6263
/// the name is prefixed with the names of the containing complex properties. For example, a complex property
6364
/// <c>Address</c> on the complex property <c>Order.Shipping</c> is mapped to the <c>Shipping_Address</c> column.
65+
/// A number is appended to the name if it's already used by another column.
6466
/// </para>
6567
/// </remarks>
6668
public static ComplexPropertyBuilder<TComplex> ToJson<TComplex>(

‎src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs‎

Lines changed: 90 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,32 +1090,16 @@ protected virtual void ValidateJsonColumns(
10901090
in StoreObjectIdentifier table,
10911091
IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)
10921092
{
1093-
var jsonColumns = new Dictionary<string, List<ITypeBase>>();
1094-
var complexProperties = new HashSet<IComplexProperty>();
1095-
var hasJsonEntityTypes = false;
1093+
ValidateJsonColumnsNotUsedByProperties(mappedTypes, table);
1094+
ValidateJsonColumnsSharing(mappedTypes, table);
10961095

1096+
var hasJsonEntityTypes = false;
10971097
foreach (var entityType in mappedTypes)
10981098
{
10991099
if (entityType.IsMappedToJson())
11001100
{
11011101
hasJsonEntityTypes = true;
11021102
ValidateJsonOwnedEntityType(entityType, table, logger);
1103-
1104-
if (!entityType.FindOwnership()!.PrincipalEntityType.IsMappedToJson())
1105-
{
1106-
Validate(entityType, entityType.GetContainerColumnName()!, jsonColumns, table);
1107-
}
1108-
1109-
continue;
1110-
}
1111-
1112-
// Inherited complex properties are reached through each type in the table, but are only mapped once
1113-
foreach (var complexProperty in entityType.GetComplexProperties())
1114-
{
1115-
if (complexProperties.Add(complexProperty))
1116-
{
1117-
ValidateComplexProperty(complexProperty, jsonColumns, table);
1118-
}
11191103
}
11201104
}
11211105

@@ -1146,37 +1130,66 @@ protected virtual void ValidateJsonColumns(
11461130

11471131
ValidateJsonEntityRoot(table, distinctRootTypes[0]);
11481132
}
1133+
}
1134+
1135+
// The JSON-mapped owned entity types and complex properties in a table or view aren't mapped to the same JSON column,
1136+
// except for complex properties on types that can't share a row
1137+
private static void ValidateJsonColumnsSharing(IReadOnlyList<IEntityType> mappedTypes, in StoreObjectIdentifier storeObject)
1138+
{
1139+
var jsonColumns = new Dictionary<string, List<ITypeBase>>();
1140+
var complexProperties = new HashSet<IComplexProperty>();
1141+
foreach (var entityType in mappedTypes)
1142+
{
1143+
if (entityType.IsMappedToJson())
1144+
{
1145+
if (!entityType.FindOwnership()!.PrincipalEntityType.IsMappedToJson())
1146+
{
1147+
Validate(entityType, entityType.GetContainerColumnName()!, jsonColumns, storeObject);
1148+
}
1149+
1150+
continue;
1151+
}
1152+
1153+
// Inherited complex properties are reached through each type in the table, but are only mapped once
1154+
foreach (var complexProperty in entityType.GetComplexProperties())
1155+
{
1156+
if (complexProperties.Add(complexProperty))
1157+
{
1158+
ValidateComplexProperty(complexProperty, jsonColumns, storeObject);
1159+
}
1160+
}
1161+
}
11491162

11501163
return;
11511164

11521165
static void ValidateComplexProperty(
11531166
IComplexProperty complexProperty,
11541167
Dictionary<string, List<ITypeBase>> jsonColumns,
1155-
in StoreObjectIdentifier table)
1168+
in StoreObjectIdentifier storeObject)
11561169
{
11571170
if (complexProperty.ComplexType.GetContainerColumnName() == null)
11581171
{
11591172
foreach (var nestedComplexProperty in complexProperty.ComplexType.GetComplexProperties())
11601173
{
1161-
ValidateComplexProperty(nestedComplexProperty, jsonColumns, table);
1174+
ValidateComplexProperty(nestedComplexProperty, jsonColumns, storeObject);
11621175
}
11631176

11641177
return;
11651178
}
11661179

11671180
// The JSON column of an inherited complex property can be in another table, e.g. with TPT
1168-
var columnName = complexProperty.ComplexType.GetContainerColumnName(table);
1181+
var columnName = complexProperty.ComplexType.GetContainerColumnName(storeObject);
11691182
if (columnName != null)
11701183
{
1171-
Validate(complexProperty.ComplexType, columnName, jsonColumns, table);
1184+
Validate(complexProperty.ComplexType, columnName, jsonColumns, storeObject);
11721185
}
11731186
}
11741187

11751188
static void Validate(
11761189
ITypeBase jsonType,
11771190
string columnName,
11781191
Dictionary<string, List<ITypeBase>> jsonColumns,
1179-
in StoreObjectIdentifier table)
1192+
in StoreObjectIdentifier storeObject)
11801193
{
11811194
if (!jsonColumns.TryGetValue(columnName, out var existingJsonTypes))
11821195
{
@@ -1192,7 +1205,7 @@ static void Validate(
11921205
// Issue #28592
11931206
// TODO: handle JSON columns on views, issue #28584
11941207
throw new InvalidOperationException(
1195-
RelationalStrings.JsonEntityMultipleRootsMappedToTheSameJsonColumn(columnName, table.Name));
1208+
RelationalStrings.JsonEntityMultipleRootsMappedToTheSameJsonColumn(columnName, storeObject.Name));
11961209
}
11971210

11981211
// Only types in different branches of the same hierarchy never share a row, so they can share the column.
@@ -1209,7 +1222,7 @@ static void Validate(
12091222
$"{existingComplexType.ComplexProperty.DeclaringType.DisplayName()}.{existingComplexType.ComplexProperty.Name}",
12101223
$"{complexType.ComplexProperty.DeclaringType.DisplayName()}.{complexType.ComplexProperty.Name}",
12111224
columnName,
1212-
table.DisplayName()));
1225+
storeObject.DisplayName()));
12131226
}
12141227

12151228
// Sibling types can't map a complex collection and a non-collection complex property to the same JSON column
@@ -1223,7 +1236,7 @@ static void Validate(
12231236
$"{complexCollection.DeclaringType.DisplayName()}.{complexCollection.Name}",
12241237
$"{complexProperty.DeclaringType.DisplayName()}.{complexProperty.Name}",
12251238
columnName,
1226-
table.DisplayName()));
1239+
storeObject.DisplayName()));
12271240
}
12281241
}
12291242

@@ -1472,6 +1485,54 @@ protected virtual void ValidateSharedTableCompatibility(
14721485
}
14731486
}
14741487

1488+
// A JSON column of a complex property can't have the name of the column of a property that isn't mapped to JSON
1489+
private static void ValidateJsonColumnsNotUsedByProperties(IReadOnlyList<IEntityType> mappedTypes, in StoreObjectIdentifier storeObject)
1490+
{
1491+
var propertyColumns = new Dictionary<string, IProperty>();
1492+
foreach (var entityType in mappedTypes)
1493+
{
1494+
foreach (var property in entityType.GetFlattenedProperties())
1495+
{
1496+
if (!property.DeclaringType.IsMappedToJson()
1497+
&& property.GetColumnName(storeObject) is { } columnName)
1498+
{
1499+
propertyColumns.TryAdd(columnName, property);
1500+
}
1501+
}
1502+
}
1503+
1504+
foreach (var entityType in mappedTypes)
1505+
{
1506+
if (!entityType.IsMappedToJson())
1507+
{
1508+
Validate(entityType, propertyColumns, storeObject);
1509+
}
1510+
}
1511+
1512+
return;
1513+
1514+
static void Validate(ITypeBase type, Dictionary<string, IProperty> propertyColumns, in StoreObjectIdentifier storeObject)
1515+
{
1516+
foreach (var complexProperty in type.GetComplexProperties())
1517+
{
1518+
if (!complexProperty.ComplexType.IsMappedToJson())
1519+
{
1520+
Validate(complexProperty.ComplexType, propertyColumns, storeObject);
1521+
}
1522+
else if (complexProperty.ComplexType.GetContainerColumnName(storeObject) is { } columnName
1523+
&& propertyColumns.TryGetValue(columnName, out var property))
1524+
{
1525+
throw new InvalidOperationException(
1526+
RelationalStrings.ComplexPropertyJsonColumnUsedByProperty(
1527+
$"{complexProperty.DeclaringType.DisplayName()}.{complexProperty.Name}",
1528+
columnName,
1529+
storeObject.DisplayName(),
1530+
$"{property.DeclaringType.DisplayName()}.{property.Name}"));
1531+
}
1532+
}
1533+
}
1534+
}
1535+
14751536
/// <summary>
14761537
/// Validates a single view and all entity types mapped to it.
14771538
/// </summary>
@@ -1488,6 +1549,8 @@ protected virtual void ValidateView(
14881549
{
14891550
ValidateSharedViewCompatibility(nonJsonMappedTypes, view.Name, view.Schema, logger);
14901551
ValidateSharedColumnsCompatibility(nonJsonMappedTypes, view, logger);
1552+
ValidateJsonColumnsNotUsedByProperties(nonJsonMappedTypes, view);
1553+
ValidateJsonColumnsSharing(nonJsonMappedTypes, view);
14911554
}
14921555

14931556
if (mappedTypes.Count != nonJsonMappedTypes.Count)

0 commit comments

Comments
 (0)