Skip to content

Commit 7cdae14

Browse files
rojiCopilot
andcommitted
Fix IsJsonCollectionColumn matching nested OwnsMany inside OwnsOne
The IsJsonCollectionColumn check was incorrectly matching a nested OwnsMany inside an OwnsOne reference, because the nested entity's GetContainerColumnName() returns the root JSON column name and its ownership has IsUnique: false. This caused Convert_string_column_to_ a_json_column_containing_required_reference to fail with '[]' instead of '{}'. Fix: only consider root-level entity mappings whose principal is not itself mapped to JSON. Added a regression test for this case. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4823084 commit 7cdae14

2 files changed

Lines changed: 55 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
@@ -1292,7 +1292,8 @@ private static bool IsJsonCollectionColumn(JsonColumn jsonColumn)
12921292
|| jsonColumn.Table.EntityTypeMappings.Any(
12931293
m => m.TypeBase is IEntityType et
12941294
&& et.GetContainerColumnName() == jsonColumn.Name
1295-
&& et.FindOwnership() is { IsUnique: false });
1295+
&& et.FindOwnership() is { IsUnique: false, PrincipalEntityType: var principal }
1296+
&& !principal.IsMappedToJson());
12961297

12971298
#endregion
12981299

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10102,6 +10102,59 @@ public virtual void Add_owned_collection_mapped_to_json_has_nullable_column()
1010210102
});
1010310103
#pragma warning restore EF8001 // Owned JSON entities are obsolete
1010410104

10105+
#pragma warning disable EF8001 // Owned JSON entities are obsolete
10106+
[ConditionalFact]
10107+
public virtual void Add_owned_reference_with_nested_collection_mapped_to_json_uses_empty_object_as_default_value()
10108+
=> Execute(
10109+
_ => { },
10110+
source =>
10111+
{
10112+
source.Entity(
10113+
"Entity", e =>
10114+
{
10115+
e.Property<int>("Id").ValueGeneratedOnAdd();
10116+
e.HasKey("Id");
10117+
});
10118+
},
10119+
target =>
10120+
{
10121+
target.Entity(
10122+
"Entity", e =>
10123+
{
10124+
e.Property<int>("Id").ValueGeneratedOnAdd();
10125+
e.HasKey("Id");
10126+
10127+
e.OwnsOne(
10128+
"Owned", "json_reference", o =>
10129+
{
10130+
o.ToJson();
10131+
o.Property<string>("Value");
10132+
o.OwnsMany(
10133+
"Nested", "NestedCollection", n =>
10134+
{
10135+
n.Property<int>("Number");
10136+
});
10137+
});
10138+
10139+
e.Navigation("json_reference").IsRequired();
10140+
});
10141+
},
10142+
upOps =>
10143+
{
10144+
Assert.Equal(1, upOps.Count);
10145+
10146+
var operation = Assert.IsType<AddColumnOperation>(upOps[0]);
10147+
Assert.Equal("Entity", operation.Table);
10148+
Assert.Equal("json_reference", operation.Name);
10149+
Assert.Equal("{}", operation.DefaultValue);
10150+
},
10151+
downOps =>
10152+
{
10153+
Assert.Equal(1, downOps.Count);
10154+
Assert.IsType<DropColumnOperation>(downOps[0]);
10155+
});
10156+
#pragma warning restore EF8001 // Owned JSON entities are obsolete
10157+
1010510158
[ConditionalFact]
1010610159
public virtual void Noop_on_complex_properties()
1010710160
=> Execute(

0 commit comments

Comments
 (0)