Skip to content

Commit 2e92772

Browse files
rojiCopilot
andcommitted
Fix IsJsonCollectionColumn for nested complex collections inside complex references
was already applied to the owned entity branch. Without this, a nested ComplexCollection inside a ComplexProperty (reference) would falsely classify the root JSON column as a collection, producing '[]' instead of '{}'. Added regression test. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 7cdae14 commit 2e92772

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,8 @@ private static bool IsJsonCollectionColumn(JsonColumn jsonColumn)
12881288
=> jsonColumn.Table.ComplexTypeMappings.Any(
12891289
m => m.TypeBase is IComplexType ct
12901290
&& ct.GetContainerColumnName() == jsonColumn.Name
1291-
&& ct.ComplexProperty.IsCollection)
1291+
&& ct.ComplexProperty.IsCollection
1292+
&& !ct.ComplexProperty.DeclaringType.IsMappedToJson())
12921293
|| jsonColumn.Table.EntityTypeMappings.Any(
12931294
m => m.TypeBase is IEntityType et
12941295
&& et.GetContainerColumnName() == jsonColumn.Name

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10053,6 +10053,54 @@ public virtual void Add_complex_collection_mapped_to_json_uses_empty_array_as_de
1005310053
Assert.IsType<DropColumnOperation>(downOps[0]);
1005410054
});
1005510055

10056+
[ConditionalFact]
10057+
public virtual void Add_complex_reference_with_nested_collection_mapped_to_json_uses_empty_object_as_default_value()
10058+
=> Execute(
10059+
_ => { },
10060+
source =>
10061+
{
10062+
source.Entity(
10063+
"Entity", e =>
10064+
{
10065+
e.Property<int>("Id").ValueGeneratedOnAdd();
10066+
e.HasKey("Id");
10067+
});
10068+
},
10069+
target =>
10070+
{
10071+
target.Entity(
10072+
"Entity", e =>
10073+
{
10074+
e.Property<int>("Id").ValueGeneratedOnAdd();
10075+
e.HasKey("Id");
10076+
10077+
e.ComplexProperty<MyJsonComplex>(
10078+
"ComplexReference", cp =>
10079+
{
10080+
cp.IsRequired();
10081+
cp.ToJson("json_reference");
10082+
cp.Property(x => x.Value);
10083+
cp.Property(x => x.Date);
10084+
cp.ComplexCollection(
10085+
x => x.NestedCollection, nc => { });
10086+
});
10087+
});
10088+
},
10089+
upOps =>
10090+
{
10091+
Assert.Equal(1, upOps.Count);
10092+
10093+
var operation = Assert.IsType<AddColumnOperation>(upOps[0]);
10094+
Assert.Equal("Entity", operation.Table);
10095+
Assert.Equal("json_reference", operation.Name);
10096+
Assert.Equal("{}", operation.DefaultValue);
10097+
},
10098+
downOps =>
10099+
{
10100+
Assert.Equal(1, downOps.Count);
10101+
Assert.IsType<DropColumnOperation>(downOps[0]);
10102+
});
10103+
1005610104
#pragma warning disable EF8001 // Owned JSON entities are obsolete
1005710105
[ConditionalFact]
1005810106
public virtual void Add_owned_collection_mapped_to_json_has_nullable_column()

0 commit comments

Comments
 (0)