11// Licensed to the .NET Foundation under one or more agreements.
22// The .NET Foundation licenses this file to you under the MIT license.
33
4+ using System . Text ;
45using System . Text . Json ;
56
67namespace Microsoft . EntityFrameworkCore . Metadata . Internal ;
@@ -13,6 +14,9 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Internal;
1314/// </summary>
1415public class RelationalModel : Annotatable , IRelationalModel
1516{
17+ internal static readonly bool UseOldBehavior32699 =
18+ AppContext . TryGetSwitch ( "Microsoft.EntityFrameworkCore.Issue32699" , out var enabled32699 ) && enabled32699 ;
19+
1620 private bool _isReadOnly ;
1721
1822 /// <summary>
@@ -340,31 +344,38 @@ private static void AddDefaultMappings(
340344 }
341345 else
342346 {
343- foreach ( var property in entityType . GetProperties ( ) )
347+ if ( UseOldBehavior32699 )
344348 {
345- var columnName = property . IsPrimaryKey ( ) || isTpc || isTph || property . DeclaringType == mappedType
346- ? property . GetColumnName ( )
347- : null ;
348- if ( columnName == null )
349+ foreach ( var property in entityType . GetProperties ( ) )
349350 {
350- continue ;
351- }
351+ var columnName = property . IsPrimaryKey ( ) || isTpc || isTph || property . DeclaringType == mappedType
352+ ? property . GetColumnName ( )
353+ : null ;
354+ if ( columnName == null )
355+ {
356+ continue ;
357+ }
352358
353- var column = ( ColumnBase < ColumnMappingBase > ? ) defaultTable . FindColumn ( columnName ) ;
354- if ( column == null )
355- {
356- column = new ColumnBase < ColumnMappingBase > ( columnName , property . GetColumnType ( ) , defaultTable )
359+ var column = ( ColumnBase < ColumnMappingBase > ? ) defaultTable . FindColumn ( columnName ) ;
360+ if ( column == null )
357361 {
358- IsNullable = property . IsColumnNullable ( )
359- } ;
360- defaultTable . Columns . Add ( columnName , column ) ;
361- }
362- else if ( ! property . IsColumnNullable ( ) )
363- {
364- column . IsNullable = false ;
365- }
362+ column = new ColumnBase < ColumnMappingBase > ( columnName , property . GetColumnType ( ) , defaultTable )
363+ {
364+ IsNullable = property . IsColumnNullable ( )
365+ } ;
366+ defaultTable . Columns . Add ( columnName , column ) ;
367+ }
368+ else if ( ! property . IsColumnNullable ( ) )
369+ {
370+ column . IsNullable = false ;
371+ }
366372
367- CreateColumnMapping ( column , property , tableMapping ) ;
373+ CreateColumnMapping ( column , property , tableMapping ) ;
374+ }
375+ }
376+ else
377+ {
378+ CreateDefaultColumnMapping ( entityType , mappedType , defaultTable , tableMapping , isTph , isTpc ) ;
368379 }
369380 }
370381
@@ -386,6 +397,83 @@ private static void AddDefaultMappings(
386397 tableMappings . Reverse ( ) ;
387398 }
388399
400+ private static void CreateDefaultColumnMapping (
401+ ITypeBase typeBase ,
402+ ITypeBase mappedType ,
403+ TableBase defaultTable ,
404+ TableMappingBase < ColumnMappingBase > tableMapping ,
405+ bool isTph ,
406+ bool isTpc )
407+ {
408+ foreach ( var property in typeBase . GetProperties ( ) )
409+ {
410+ var columnName = property . IsPrimaryKey ( ) || isTpc || isTph || property . DeclaringType == mappedType
411+ ? GetColumnName ( property )
412+ : null ;
413+
414+ if ( columnName == null )
415+ {
416+ continue ;
417+ }
418+
419+ var column = ( ColumnBase < ColumnMappingBase > ? ) defaultTable . FindColumn ( columnName ) ;
420+ if ( column == null )
421+ {
422+ column = new ColumnBase < ColumnMappingBase > ( columnName , property . GetColumnType ( ) , defaultTable )
423+ {
424+ IsNullable = property . IsColumnNullable ( )
425+ } ;
426+ defaultTable . Columns . Add ( columnName , column ) ;
427+ }
428+ else if ( ! property . IsColumnNullable ( ) )
429+ {
430+ column . IsNullable = false ;
431+ }
432+
433+ CreateColumnMapping ( column , property , tableMapping ) ;
434+ }
435+
436+ foreach ( var complexProperty in typeBase . GetDeclaredComplexProperties ( ) )
437+ {
438+ var complexType = complexProperty . ComplexType ;
439+ tableMapping = new TableMappingBase < ColumnMappingBase > ( complexType , defaultTable , includesDerivedTypes : false ) ;
440+
441+ CreateDefaultColumnMapping ( complexType , complexType , defaultTable , tableMapping , isTph , isTpc ) ;
442+
443+ var tableMappings = ( List < TableMappingBase < ColumnMappingBase > > ? ) complexType
444+ . FindRuntimeAnnotationValue ( RelationalAnnotationNames . DefaultMappings ) ;
445+ if ( tableMappings == null )
446+ {
447+ tableMappings = new List < TableMappingBase < ColumnMappingBase > > ( ) ;
448+ complexType . AddRuntimeAnnotation ( RelationalAnnotationNames . DefaultMappings , tableMappings ) ;
449+ }
450+ tableMappings . Add ( tableMapping ) ;
451+
452+ defaultTable . ComplexTypeMappings . Add ( tableMapping ) ;
453+ }
454+
455+ static string GetColumnName ( IProperty property )
456+ {
457+ var complexType = property . DeclaringType as IComplexType ;
458+ if ( complexType != null )
459+ {
460+ var builder = new StringBuilder ( ) ;
461+ builder . Append ( property . Name ) ;
462+ while ( complexType != null )
463+ {
464+ builder . Insert ( 0 , "_" ) ;
465+ builder . Insert ( 0 , complexType . ComplexProperty . Name ) ;
466+
467+ complexType = complexType . ComplexProperty . DeclaringType as IComplexType ;
468+ }
469+
470+ return builder . ToString ( ) ;
471+ }
472+
473+ return property . GetColumnName ( ) ;
474+ }
475+ }
476+
389477 private static void AddTables (
390478 RelationalModel databaseModel ,
391479 IEntityType entityType ,
0 commit comments