File tree Expand file tree Collapse file tree
src/EFCore.Relational/Migrations/Internal
test/EFCore.Relational.Tests/Migrations/Internal Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1279,11 +1279,21 @@ private void InitializeJsonColumn(
12791279 columnOperation . ClrType = typeof ( string ) ;
12801280 columnOperation . DefaultValue = inline || isNullable
12811281 ? null
1282- : "{}" ;
1282+ : IsJsonCollectionColumn ( jsonColumn ) ? "[]" : "{}" ;
12831283
12841284 columnOperation . AddAnnotations ( migrationsAnnotations ) ;
12851285 }
12861286
1287+ private static bool IsJsonCollectionColumn ( JsonColumn jsonColumn )
1288+ => jsonColumn . Table . ComplexTypeMappings . Any (
1289+ m => m . TypeBase is IComplexType ct
1290+ && ct . GetContainerColumnName ( ) == jsonColumn . Name
1291+ && ct . ComplexProperty . IsCollection )
1292+ || jsonColumn . Table . EntityTypeMappings . Any (
1293+ m => m . TypeBase is IEntityType et
1294+ && et . GetContainerColumnName ( ) == jsonColumn . Name
1295+ && et . FindOwnership ( ) is { IsUnique : false } ) ;
1296+
12871297 #endregion
12881298
12891299 #region IKey
Original file line number Diff line number Diff line change @@ -10007,6 +10007,52 @@ public virtual void Convert_table_from_owned_to_complex_properties_mapped_to_jso
1000710007 Assert . Empty ) ;
1000810008#pragma warning restore EF8001 // Owned JSON entities are obsolete
1000910009
10010+ [ ConditionalFact ]
10011+ public virtual void Add_complex_collection_mapped_to_json_uses_empty_array_as_default_value ( )
10012+ => Execute (
10013+ _ => { } ,
10014+ source =>
10015+ {
10016+ source . Entity (
10017+ "Entity" , e =>
10018+ {
10019+ e . Property < int > ( "Id" ) . ValueGeneratedOnAdd ( ) ;
10020+ e . HasKey ( "Id" ) ;
10021+ } ) ;
10022+ } ,
10023+ target =>
10024+ {
10025+ target . Entity (
10026+ "Entity" , e =>
10027+ {
10028+ e . Property < int > ( "Id" ) . ValueGeneratedOnAdd ( ) ;
10029+ e . HasKey ( "Id" ) ;
10030+
10031+ e . ComplexCollection < List < MyJsonComplex > , MyJsonComplex > (
10032+ "ComplexCollection" , cp =>
10033+ {
10034+ cp . IsRequired ( ) ;
10035+ cp . ToJson ( "json_collection" ) ;
10036+ cp . Property ( x => x . Value ) ;
10037+ cp . Property ( x => x . Date ) ;
10038+ } ) ;
10039+ } ) ;
10040+ } ,
10041+ upOps =>
10042+ {
10043+ Assert . Equal ( 1 , upOps . Count ) ;
10044+
10045+ var operation = Assert . IsType < AddColumnOperation > ( upOps [ 0 ] ) ;
10046+ Assert . Equal ( "Entity" , operation . Table ) ;
10047+ Assert . Equal ( "json_collection" , operation . Name ) ;
10048+ Assert . Equal ( "[]" , operation . DefaultValue ) ;
10049+ } ,
10050+ downOps =>
10051+ {
10052+ Assert . Equal ( 1 , downOps . Count ) ;
10053+ Assert . IsType < DropColumnOperation > ( downOps [ 0 ] ) ;
10054+ } ) ;
10055+
1001010056 [ ConditionalFact ]
1001110057 public virtual void Noop_on_complex_properties ( )
1001210058 => Execute (
You can’t perform that action at this time.
0 commit comments