Skip to content

Commit 55a935c

Browse files
Copilotroji
andcommitted
Fix ComplexCollection ToJson default value in migrations to use [] instead of {}
Co-authored-by: roji <1862641+roji@users.noreply.github.com> Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/1fd85ba2-cb75-4e30-b1f5-afbf64cc4626
1 parent aeda6f5 commit 55a935c

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff 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

test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff 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(

0 commit comments

Comments
 (0)