@@ -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