@@ -160,7 +160,7 @@ public static IRelationalModel Create(
160160
161161        foreach  ( var  entityType  in  model . GetEntityTypes ( ) ) 
162162        { 
163-             AddDefaultMappings ( databaseModel ,  entityType ) ; 
163+             AddDefaultMappings ( databaseModel ,  entityType ,   relationalTypeMappingSource ) ; 
164164
165165            AddTables ( databaseModel ,  entityType ,  relationalTypeMappingSource ) ; 
166166
@@ -291,7 +291,10 @@ public static IRelationalModel Create(
291291        return  databaseModel ; 
292292    } 
293293
294-     private  static void  AddDefaultMappings ( RelationalModel  databaseModel ,  IEntityType  entityType ) 
294+     private  static void  AddDefaultMappings ( 
295+         RelationalModel  databaseModel , 
296+         IEntityType  entityType , 
297+         IRelationalTypeMappingSource  relationalTypeMappingSource ) 
295298    { 
296299        var  mappedType  =  entityType ; 
297300        Check . DebugAssert ( entityType . FindRuntimeAnnotationValue ( RelationalAnnotationNames . DefaultMappings )  ==  null ,  "not null" ) ; 
@@ -312,42 +315,52 @@ private static void AddDefaultMappings(RelationalModel databaseModel, IEntityTyp
312315            var  tableMapping  =  new  TableMappingBase < ColumnMappingBase > ( 
313316                entityType ,  defaultTable ,  includesDerivedTypes :  ! isTpc  &&  mappedType  ==  entityType ) ; 
314317
315-             foreach  ( var  property  in  entityType . GetProperties ( ) ) 
318+             var  containerColumnName  =  mappedType . GetContainerColumnName ( ) ; 
319+             if  ( ! string . IsNullOrEmpty ( containerColumnName ) ) 
316320            { 
317-                 var  columnName  =  property . IsPrimaryKey ( )  ||  isTpc  ||  isTph  ||  property . DeclaringEntityType  ==  mappedType 
318-                     ?  property . GetColumnName ( ) 
319-                     :  null ; 
320-                 if  ( columnName  ==  null ) 
321+                 CreateContainerColumn ( 
322+                     defaultTable ,  containerColumnName ,  mappedType ,  relationalTypeMappingSource , 
323+                     static  ( c ,  t ,  m )  =>  new  ColumnBase < ColumnMappingBase > ( c ,  m . StoreType ,  t ,  m ) ) ; 
324+             } 
325+             else 
326+             { 
327+                 foreach  ( var  property  in  entityType . GetProperties ( ) ) 
321328                { 
322-                     continue ; 
323-                 } 
329+                     var  columnName  =  property . IsPrimaryKey ( )  ||  isTpc  ||  isTph  ||  property . DeclaringEntityType  ==  mappedType 
330+                         ?  property . GetColumnName ( ) 
331+                         :  null ; 
332+                     if  ( columnName  ==  null ) 
333+                     { 
334+                         continue ; 
335+                     } 
324336
325-                 var  column  =  ( ColumnBase < ColumnMappingBase > ? ) defaultTable . FindColumn ( columnName ) ; 
326-                 if  ( column  ==  null ) 
327-                 { 
328-                     column  =  new  ColumnBase < ColumnMappingBase > ( columnName ,  property . GetColumnType ( ) ,  defaultTable ) 
337+                     var  column  =  ( ColumnBase < ColumnMappingBase > ? ) defaultTable . FindColumn ( columnName ) ; 
338+                     if  ( column  ==  null ) 
329339                    { 
330-                         IsNullable  =  property . IsColumnNullable ( ) 
331-                     } ; 
332-                     defaultTable . Columns . Add ( columnName ,  column ) ; 
333-                 } 
334-                 else  if  ( ! property . IsColumnNullable ( ) ) 
335-                 { 
336-                     column . IsNullable  =  false ; 
337-                 } 
340+                         column  =  new  ColumnBase < ColumnMappingBase > ( columnName ,  property . GetColumnType ( ) ,  defaultTable ) 
341+                         { 
342+                             IsNullable  =  property . IsColumnNullable ( ) 
343+                         } ; 
344+                         defaultTable . Columns . Add ( columnName ,  column ) ; 
345+                     } 
346+                     else  if  ( ! property . IsColumnNullable ( ) ) 
347+                     { 
348+                         column . IsNullable  =  false ; 
349+                     } 
338350
339-                 var  columnMapping  =  new  ColumnMappingBase ( property ,  column ,  tableMapping ) ; 
340-                 tableMapping . AddColumnMapping ( columnMapping ) ; 
341-                 column . AddPropertyMapping ( columnMapping ) ; 
351+                      var  columnMapping  =  new  ColumnMappingBase ( property ,  column ,  tableMapping ) ; 
352+                      tableMapping . AddColumnMapping ( columnMapping ) ; 
353+                      column . AddPropertyMapping ( columnMapping ) ; 
342354
343-                 if  ( property . FindRuntimeAnnotationValue ( RelationalAnnotationNames . DefaultColumnMappings ) 
344-                     is  not SortedSet < ColumnMappingBase >  columnMappings ) 
345-                 { 
346-                     columnMappings  =  new  SortedSet < ColumnMappingBase > ( ColumnMappingBaseComparer . Instance ) ; 
347-                     property . AddRuntimeAnnotation ( RelationalAnnotationNames . DefaultColumnMappings ,  columnMappings ) ; 
348-                 } 
355+                      if  ( property . FindRuntimeAnnotationValue ( RelationalAnnotationNames . DefaultColumnMappings ) 
356+                          is  not SortedSet < ColumnMappingBase >  columnMappings ) 
357+                      { 
358+                          columnMappings  =  new  SortedSet < ColumnMappingBase > ( ColumnMappingBaseComparer . Instance ) ; 
359+                          property . AddRuntimeAnnotation ( RelationalAnnotationNames . DefaultColumnMappings ,  columnMappings ) ; 
360+                      } 
349361
350-                 columnMappings . Add ( columnMapping ) ; 
362+                     columnMappings . Add ( columnMapping ) ; 
363+                 } 
351364            } 
352365
353366            if  ( ( ( ITableMappingBase ) tableMapping ) . ColumnMappings . Any ( ) 
@@ -460,23 +473,9 @@ private static void CreateTableMapping(
460473        var  containerColumnName  =  mappedType . GetContainerColumnName ( ) ; 
461474        if  ( ! string . IsNullOrEmpty ( containerColumnName ) ) 
462475        { 
463-             var  ownership  =  mappedType . GetForeignKeys ( ) . Single ( fk =>  fk . IsOwnership ) ; 
464-             if  ( ! ownership . PrincipalEntityType . IsMappedToJson ( ) ) 
465-             { 
466-                 Debug . Assert ( table . FindColumn ( containerColumnName )  ==  null ) ; 
467- 
468-                 var  jsonColumnTypeMapping  =  relationalTypeMappingSource . FindMapping ( typeof ( JsonElement ) ) ! ; 
469-                 var  jsonColumn  =  new  JsonColumn ( 
470-                     containerColumnName ,  jsonColumnTypeMapping . StoreType ,  table ,  jsonColumnTypeMapping . ProviderValueComparer ) ; 
471-                 table . Columns . Add ( containerColumnName ,  jsonColumn ) ; 
472-                 jsonColumn . IsNullable  =  ! ownership . IsRequiredDependent  ||  ! ownership . IsUnique ; 
473- 
474-                 if  ( ownership . PrincipalEntityType . BaseType  !=  null ) 
475-                 { 
476-                     // if navigation is defined on a derived type, the column must be made nullable 
477-                     jsonColumn . IsNullable  =  true ; 
478-                 } 
479-             } 
476+             CreateContainerColumn ( 
477+                 table ,  containerColumnName ,  mappedType ,  relationalTypeMappingSource , 
478+                 static  ( c ,  t ,  m )  =>  new  JsonColumn ( c ,  ( Table ) t ,  m ) ) ; 
480479        } 
481480        else 
482481        { 
@@ -526,6 +525,32 @@ private static void CreateTableMapping(
526525        } 
527526    } 
528527
528+     private  static void  CreateContainerColumn < TColumnMappingBase > ( 
529+         TableBase  tableBase , 
530+         string  containerColumnName , 
531+         IEntityType  mappedType , 
532+         IRelationalTypeMappingSource  relationalTypeMappingSource , 
533+         Func < string ,  TableBase ,  RelationalTypeMapping ,  ColumnBase < TColumnMappingBase > >  createColumn ) 
534+         where  TColumnMappingBase  :  class ,  IColumnMappingBase 
535+     { 
536+         var  ownership  =  mappedType . GetForeignKeys ( ) . Single ( fk =>  fk . IsOwnership ) ; 
537+         if  ( ! ownership . PrincipalEntityType . IsMappedToJson ( ) ) 
538+         { 
539+             Check . DebugAssert ( tableBase . FindColumn ( containerColumnName )  ==  null ,  $ "Table does not have column '{ containerColumnName } '.") ; 
540+ 
541+             var  jsonColumnTypeMapping  =  relationalTypeMappingSource . FindMapping ( typeof ( JsonElement ) ) ! ; 
542+             var  jsonColumn  =  createColumn ( containerColumnName ,  tableBase ,  jsonColumnTypeMapping ) ; // new JsonColumn(containerColumnName, table, jsonColumnTypeMapping); 
543+             tableBase . Columns . Add ( containerColumnName ,  jsonColumn ) ; 
544+             jsonColumn . IsNullable  =  ! ownership . IsRequiredDependent  ||  ! ownership . IsUnique ; 
545+ 
546+             if  ( ownership . PrincipalEntityType . BaseType  !=  null ) 
547+             { 
548+                 // if navigation is defined on a derived type, the column must be made nullable 
549+                 jsonColumn . IsNullable  =  true ; 
550+             } 
551+         } 
552+     } 
553+ 
529554    private  static void  AddViews ( 
530555        RelationalModel  databaseModel , 
531556        IEntityType  entityType , 
@@ -618,22 +643,9 @@ private static void CreateViewMapping(
618643        var  containerColumnName  =  mappedType . GetContainerColumnName ( ) ; 
619644        if  ( ! string . IsNullOrEmpty ( containerColumnName ) ) 
620645        { 
621-             var  ownership  =  mappedType . GetForeignKeys ( ) . Single ( fk =>  fk . IsOwnership ) ; 
622-             if  ( ! ownership . PrincipalEntityType . IsMappedToJson ( ) ) 
623-             { 
624-                 Debug . Assert ( view . FindColumn ( containerColumnName )  ==  null ) ; 
625- 
626-                 var  jsonColumnTypeMapping  =  relationalTypeMappingSource . FindMapping ( typeof ( JsonElement ) ) ! ; 
627-                 var  jsonColumn  =  new  JsonViewColumn ( containerColumnName ,  jsonColumnTypeMapping . StoreType ,  view ) ; 
628-                 view . Columns . Add ( containerColumnName ,  jsonColumn ) ; 
629-                 jsonColumn . IsNullable  =  ! ownership . IsRequired  ||  ! ownership . IsUnique ; 
630- 
631-                 if  ( ownership . PrincipalEntityType . BaseType  !=  null ) 
632-                 { 
633-                     // if navigation is defined on a derived type, the column must be made nullable 
634-                     jsonColumn . IsNullable  =  true ; 
635-                 } 
636-             } 
646+             CreateContainerColumn ( 
647+                 view ,  containerColumnName ,  mappedType ,  relationalTypeMappingSource , 
648+                 static  ( c ,  t ,  m )  =>  new  JsonViewColumn ( c ,  ( View ) t ,  m ) ) ; 
637649        } 
638650        else 
639651        { 
0 commit comments