@@ -135,12 +135,12 @@ public static IRelationalModel Create(
135135
136136 AddSqlQueries ( databaseModel , entityType ) ;
137137
138- AddMappedFunctions ( databaseModel , entityType ) ;
138+ AddMappedFunctions ( databaseModel , entityType , relationalTypeMappingSource ) ;
139139
140140 AddStoredProcedures ( databaseModel , entityType , relationalTypeMappingSource ) ;
141141 }
142142
143- AddTvfs ( databaseModel ) ;
143+ AddTvfs ( databaseModel , relationalTypeMappingSource ) ;
144144
145145 var tables = ( ( IRelationalModel ) databaseModel ) . Tables ;
146146 foreach ( Table table in tables )
@@ -918,7 +918,7 @@ private static void AddSqlQueries(RelationalModel databaseModel, IEntityType ent
918918 queryMappings ? . Reverse ( ) ;
919919 }
920920
921- private static void AddMappedFunctions ( RelationalModel databaseModel , IEntityType entityType )
921+ private static void AddMappedFunctions ( RelationalModel databaseModel , IEntityType entityType , IRelationalTypeMappingSource relationalTypeMappingSource )
922922 {
923923 var model = databaseModel . Model ;
924924 var functionName = entityType . GetFunctionName ( ) ;
@@ -940,7 +940,7 @@ private static void AddMappedFunctions(RelationalModel databaseModel, IEntityTyp
940940 }
941941
942942 var dbFunction = ( IRuntimeDbFunction ) model . FindDbFunction ( mappedFunctionName ) ! ;
943- var functionMapping = CreateFunctionMapping ( entityType , mappedType , dbFunction , databaseModel , @default : true ) ;
943+ var functionMapping = CreateFunctionMapping ( entityType , mappedType , dbFunction , databaseModel , relationalTypeMappingSource , @default : true ) ;
944944
945945 mappedType = mappedType . BaseType ;
946946
@@ -963,7 +963,7 @@ private static void AddMappedFunctions(RelationalModel databaseModel, IEntityTyp
963963 functionMappings ? . Reverse ( ) ;
964964 }
965965
966- private static void AddTvfs ( RelationalModel relationalModel )
966+ private static void AddTvfs ( RelationalModel relationalModel , IRelationalTypeMappingSource relationalTypeMappingSource )
967967 {
968968 var model = relationalModel . Model ;
969969 foreach ( IRuntimeDbFunction function in model . GetDbFunctions ( ) )
@@ -982,25 +982,45 @@ private static void AddTvfs(RelationalModel relationalModel)
982982 continue ;
983983 }
984984
985- var functionMapping = CreateFunctionMapping ( entityType , entityType , function , relationalModel , @default : false ) ;
985+ AddTvfMapping ( entityType , function , relationalModel , relationalTypeMappingSource ) ;
986986
987- if ( entityType . FindRuntimeAnnotationValue ( RelationalAnnotationNames . FunctionMappings )
988- is not List < FunctionMapping > functionMappings )
987+ foreach ( var ownedJsonNavigation in entityType . GetNavigationsInHierarchy ( )
988+ . Where (
989+ n => n . ForeignKey . IsOwnership
990+ && n . TargetEntityType . IsMappedToJson ( )
991+ && n . ForeignKey . PrincipalToDependent == n ) )
989992 {
990- functionMappings = [ ] ;
991- entityType . AddRuntimeAnnotation ( RelationalAnnotationNames . FunctionMappings , functionMappings ) ;
993+ AddTvfMapping ( ownedJsonNavigation . TargetEntityType , function , relationalModel , relationalTypeMappingSource ) ;
992994 }
995+ }
996+ }
993997
994- functionMappings . Add ( functionMapping ) ;
995- ( ( StoreFunction ) functionMapping . StoreFunction ) . EntityTypeMappings . Add ( functionMapping ) ;
998+ private static void AddTvfMapping (
999+ IEntityType entityType ,
1000+ IRuntimeDbFunction function ,
1001+ RelationalModel relationalModel ,
1002+ IRelationalTypeMappingSource relationalTypeMappingSource )
1003+ {
1004+ var functionMapping = CreateFunctionMapping (
1005+ entityType , entityType , function , relationalModel , relationalTypeMappingSource , @default : false ) ;
1006+
1007+ if ( entityType . FindRuntimeAnnotationValue ( RelationalAnnotationNames . FunctionMappings )
1008+ is not List < FunctionMapping > functionMappings )
1009+ {
1010+ functionMappings = [ ] ;
1011+ entityType . AddRuntimeAnnotation ( RelationalAnnotationNames . FunctionMappings , functionMappings ) ;
9961012 }
1013+
1014+ functionMappings . Add ( functionMapping ) ;
1015+ ( ( StoreFunction ) functionMapping . StoreFunction ) . EntityTypeMappings . Add ( functionMapping ) ;
9971016 }
9981017
9991018 private static FunctionMapping CreateFunctionMapping (
10001019 IEntityType entityType ,
10011020 IEntityType mappedType ,
10021021 IRuntimeDbFunction dbFunction ,
10031022 RelationalModel model ,
1023+ IRelationalTypeMappingSource relationalTypeMappingSource ,
10041024 bool @default )
10051025 {
10061026 var storeFunction = GetOrCreateStoreFunction ( dbFunction , model ) ;
@@ -1010,29 +1030,41 @@ private static FunctionMapping CreateFunctionMapping(
10101030 entityType , storeFunction , dbFunction ,
10111031 includesDerivedTypes : entityType . GetDirectlyDerivedTypes ( ) . Any ( ) ? true : null ) { IsDefaultFunctionMapping = @default } ;
10121032
1013- foreach ( var property in mappedType . GetProperties ( ) )
1033+ var containerColumnName = mappedType . GetContainerColumnName ( ) ;
1034+ var containerColumnType = mappedType . GetContainerColumnType ( ) ;
1035+ if ( ! string . IsNullOrEmpty ( containerColumnName ) )
10141036 {
1015- var columnName = property . GetColumnName ( mappedFunction ) ;
1016- if ( columnName == null )
1037+ CreateContainerColumn (
1038+ storeFunction , containerColumnName , containerColumnType , mappedType , relationalTypeMappingSource ,
1039+ static ( colName , colType , table , mapping )
1040+ => new FunctionColumn ( colName , colType ?? mapping . StoreType , ( StoreFunction ) table , mapping ) ) ;
1041+ }
1042+ else
1043+ {
1044+ foreach ( var property in mappedType . GetProperties ( ) )
10171045 {
1018- continue ;
1019- }
1046+ var columnName = property . GetColumnName ( mappedFunction ) ;
1047+ if ( columnName == null )
1048+ {
1049+ continue ;
1050+ }
10201051
1021- var column = storeFunction . FindColumn ( columnName ) ;
1022- if ( column == null )
1023- {
1024- column = new FunctionColumn ( columnName , property . GetColumnType ( mappedFunction ) , storeFunction )
1052+ var column = storeFunction . FindColumn ( columnName ) ;
1053+ if ( column == null )
10251054 {
1026- IsNullable = property . IsColumnNullable ( mappedFunction )
1027- } ;
1028- storeFunction . Columns . Add ( columnName , column ) ;
1029- }
1030- else if ( ! property . IsColumnNullable ( mappedFunction ) )
1031- {
1032- column . IsNullable = false ;
1033- }
1055+ column = new FunctionColumn ( columnName , property . GetColumnType ( mappedFunction ) , storeFunction )
1056+ {
1057+ IsNullable = property . IsColumnNullable ( mappedFunction )
1058+ } ;
1059+ storeFunction . Columns . Add ( columnName , column ) ;
1060+ }
1061+ else if ( ! property . IsColumnNullable ( mappedFunction ) )
1062+ {
1063+ column . IsNullable = false ;
1064+ }
10341065
1035- CreateFunctionColumnMapping ( column , property , functionMapping ) ;
1066+ CreateFunctionColumnMapping ( column , property , functionMapping ) ;
1067+ }
10361068 }
10371069
10381070 return functionMapping ;
0 commit comments