@@ -500,6 +500,91 @@ private void CreateJsonElements(
500500 }
501501 }
502502
503+ private void CreateJsonElementMappings (
504+ ITableMappingBase tableMapping ,
505+ string tableMappingVariable ,
506+ CSharpRuntimeAnnotationCodeGeneratorParameters parameters )
507+ {
508+ foreach ( var column in tableMapping . Table . Columns )
509+ {
510+ if ( column . JsonElement != null )
511+ {
512+ CreateJsonElementMappings ( column . JsonElement , tableMapping , tableMappingVariable , parameters ) ;
513+ }
514+ }
515+ }
516+
517+ private void CreateJsonElementMappings (
518+ IRelationalJsonElement element ,
519+ ITableMappingBase tableMapping ,
520+ string tableMappingVariable ,
521+ CSharpRuntimeAnnotationCodeGeneratorParameters parameters )
522+ {
523+ if ( parameters . ScopeVariables . TryGetValue ( element , out var elementVariable ) )
524+ {
525+ foreach ( var mapping in element . PropertyMappings . Where ( m => ReferenceEquals ( m . TableMapping , tableMapping ) ) )
526+ {
527+ parameters . MainBuilder
528+ . Append ( "RelationalModel.CreateJsonElementMapping(" )
529+ . Append ( GetPropertyBaseAccess ( mapping . Property , parameters ) )
530+ . Append ( ", " )
531+ . Append ( elementVariable )
532+ . Append ( ", " )
533+ . Append ( tableMappingVariable )
534+ . AppendLine ( ");" ) ;
535+ }
536+ }
537+
538+ switch ( element )
539+ {
540+ case IRelationalJsonObject jsonObject :
541+ foreach ( var child in jsonObject . Properties )
542+ {
543+ CreateJsonElementMappings ( child , tableMapping , tableMappingVariable , parameters ) ;
544+ }
545+
546+ break ;
547+ case IRelationalJsonArray jsonArray :
548+ CreateJsonElementMappings ( jsonArray . ElementType , tableMapping , tableMappingVariable , parameters ) ;
549+ break ;
550+ }
551+ }
552+
553+ private string GetPropertyBaseAccess (
554+ IPropertyBase propertyBase ,
555+ CSharpRuntimeAnnotationCodeGeneratorParameters parameters )
556+ {
557+ var code = Dependencies . CSharpHelper ;
558+ var declaringTypeAccess = GetTypeBaseAccess ( propertyBase . DeclaringType , parameters ) ;
559+
560+ return propertyBase switch
561+ {
562+ IProperty property => $ "{ declaringTypeAccess } .FindProperty({ code . Literal ( property . Name ) } )!",
563+ IComplexProperty complexProperty => $ "{ declaringTypeAccess } .FindComplexProperty({ code . Literal ( complexProperty . Name ) } )!",
564+ INavigation navigation => $ "{ declaringTypeAccess } .FindNavigation({ code . Literal ( navigation . Name ) } )!",
565+ _ => throw new UnreachableException ( )
566+ } ;
567+ }
568+
569+ private string GetTypeBaseAccess (
570+ ITypeBase typeBase ,
571+ CSharpRuntimeAnnotationCodeGeneratorParameters parameters )
572+ {
573+ var code = Dependencies . CSharpHelper ;
574+ if ( parameters . ScopeVariables . TryGetValue ( typeBase , out var variable ) )
575+ {
576+ return variable ;
577+ }
578+
579+ return typeBase switch
580+ {
581+ IEntityType entityType => $ "FindEntityType({ code . Literal ( entityType . Name ) } )!",
582+ IComplexType complexType
583+ => $ "{ GetTypeBaseAccess ( complexType . ComplexProperty . DeclaringType , parameters ) } .FindComplexProperty({ code . Literal ( complexType . ComplexProperty . Name ) } )!.ComplexType",
584+ _ => throw new UnreachableException ( )
585+ } ;
586+ }
587+
503588 private string CreateJsonElement (
504589 IRelationalJsonElement element ,
505590 string columnVariable ,
@@ -1317,6 +1402,8 @@ private void Create(
13171402 . Append ( $ "{ typeBaseVariable } .FindProperty({ code . Literal ( columnMapping . Property . Name ) } )!, ")
13181403 . Append ( tableMappingVariable ) . AppendLine ( ");" ) ;
13191404 }
1405+
1406+ CreateJsonElementMappings ( tableMapping , tableMappingVariable , parameters ) ;
13201407 }
13211408
13221409 /// <summary>
@@ -1369,6 +1456,8 @@ private void Create(
13691456 . Append ( tableMappingVariable ) . AppendLine ( ");" ) ;
13701457 }
13711458
1459+ CreateJsonElementMappings ( tableMapping , tableMappingVariable , parameters ) ;
1460+
13721461 if ( tableMapping == table . EntityTypeMappings . Last ( ) )
13731462 {
13741463 foreach ( var uniqueConstraint in table . UniqueConstraints )
@@ -1437,6 +1526,8 @@ private void Create(
14371526 . Append ( $ "{ typeBaseVariable } .FindProperty({ code . Literal ( columnMapping . Property . Name ) } )!, ")
14381527 . Append ( viewMappingVariable ) . AppendLine ( ");" ) ;
14391528 }
1529+
1530+ CreateJsonElementMappings ( viewMapping , viewMappingVariable , parameters ) ;
14401531 }
14411532
14421533 /// <summary>
@@ -1490,6 +1581,8 @@ private void Create(
14901581 . Append ( $ "{ typeBaseVariable } .FindProperty({ code . Literal ( columnMapping . Property . Name ) } )!, ")
14911582 . Append ( sqlQueryMappingVariable ) . AppendLine ( ");" ) ;
14921583 }
1584+
1585+ CreateJsonElementMappings ( sqlQueryMapping , sqlQueryMappingVariable , parameters ) ;
14931586 }
14941587
14951588 /// <summary>
@@ -1545,6 +1638,8 @@ private void Create(
15451638 . Append ( $ "{ typeBaseVariable } .FindProperty({ code . Literal ( columnMapping . Property . Name ) } )!, ")
15461639 . Append ( functionMappingVariable ) . AppendLine ( ");" ) ;
15471640 }
1641+
1642+ CreateJsonElementMappings ( functionMapping , functionMappingVariable , parameters ) ;
15481643 }
15491644
15501645 /// <summary>
0 commit comments