Skip to content

Commit f4f8c31

Browse files
authored
Merge pull request #123 from microting/copilot/fix-tojson-issues
Add ToJson() configuration for complex collections in EF Core 10
2 parents c0828c2 + 81e3915 commit f4f8c31

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

test/EFCore.MySql.FunctionalTests/ComplexCollectionJsonMySqlTest.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
123123
{
124124
entity.HasKey(e => e.Id);
125125

126-
// In EF Core 10, complex collections MUST use ToJson()
127-
// This maps the collection to a JSON column in the database
128-
// We use ComplexProperty to configure the Departments complex type collection
129-
entity.ComplexProperty(e => e.Departments);
130-
//.ToJson(); // This should make it a JSON column - but the API might be different
131-
132-
// Alternative: If ComplexProperty doesn't have ToJson(), we might need to use:
133-
// entity.Property(e => e.Departments).ToJson();
126+
// In EF Core 10, complex collections MUST be mapped to JSON columns
127+
entity.ComplexCollection(e => e.Departments).ToJson();
134128
});
135129
}
136130
}

test/EFCore.MySql.FunctionalTests/ComplexTypesTrackingMySqlTest.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,51 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
3636
{
3737
base.OnModelCreating(modelBuilder, context);
3838

39+
// Complex collections must be mapped to JSON columns in EF Core 10+
40+
modelBuilder.Entity<PubWithCollections>(b =>
41+
{
42+
b.ComplexCollection(
43+
e => e.Activities, b => b.ToJson());
44+
});
45+
46+
modelBuilder.Entity<PubWithRecordCollections>(b =>
47+
{
48+
b.ComplexCollection(
49+
e => e.Activities, b => b.ToJson());
50+
});
51+
52+
modelBuilder.Entity<PubWithArrayCollections>(b =>
53+
{
54+
b.ComplexCollection(
55+
e => e.Activities, b => b.ToJson());
56+
});
57+
58+
modelBuilder.Entity<PubWithRecordArrayCollections>(b =>
59+
{
60+
b.ComplexCollection(
61+
e => e.Activities, b => b.ToJson());
62+
});
63+
64+
modelBuilder.Entity<PubWithPropertyBagCollections>(b =>
65+
{
66+
b.ComplexCollection(
67+
e => e.Activities, b => b.ToJson());
68+
});
69+
70+
// Field-based entities are only configured when not using proxies
71+
// (proxies require virtual properties)
72+
modelBuilder.Entity<FieldPubWithCollections>(b =>
73+
{
74+
b.ComplexCollection(
75+
e => e.Activities, b => b.ToJson());
76+
});
77+
78+
modelBuilder.Entity<FieldPubWithRecordCollections>(b =>
79+
{
80+
b.ComplexCollection(
81+
e => e.Activities, b => b.ToJson());
82+
});
83+
3984
// modelBuilder.Entity<Pub>(
4085
// b =>
4186
// {

test/EFCore.MySql.FunctionalTests/PropertyValuesMySqlTest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ public PropertyValuesMySqlTest(PropertyValuesMySqlFixture fixture)
1414
public class PropertyValuesMySqlFixture : PropertyValuesFixtureBase
1515
{
1616
protected override ITestStoreFactory TestStoreFactory => MySqlTestStoreFactory.Instance;
17+
18+
protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext context)
19+
{
20+
base.OnModelCreating(modelBuilder, context);
21+
22+
// Complex collections must be mapped to JSON columns in EF Core 10+
23+
modelBuilder.Entity<School>(b => b.ComplexCollection(e => e.Departments, b => b.ToJson()));
24+
}
1725
}
1826
}
1927
}

0 commit comments

Comments
 (0)