You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I seem to be unable to serialize dictionaries in a JSON column with the new JSON support for SQL Server
usingSystem.Text.Json;usingMicrosoft.EntityFrameworkCore;usingTestContextcontext=new();Thingthing1=new(){Data=newDictionary<string,object>{{"test","1"}},Child=newChildThing{Name="Foo",Data=newDictionary<string,string>{{"test","2"}}}};Console.WriteLine(JsonSerializer.Serialize(thing1));//just test that the object can be serialized to JSON outside EFcontext.Things.Add(thing1);context.SaveChanges();usingTestContextcontext2=new();List<Thing>things=context2.Things.ToList();foreach(Thingthinginthings){Console.WriteLine($"Thing: {thing.ThingId},\n Thing.Data:{thing.Data.First().Key}:{thing.Data.First().Value}\n Child.Name: {thing.Child!.Name}, Child.Data.Count: {thing.Child?.Data?.Count}");}publicclassTestContext:DbContext{protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptions){options.UseSqlServer("Data Source=.\\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=True;MultipleActiveResultSets=true;TrustServerCertificate=True");}protectedoverridevoidOnModelCreating(ModelBuilderbuilder){builder.Entity<Thing>().OwnsOne(thing =>thing.Child, ownedNavigationBuilder =>{ownedNavigationBuilder.ToJson();ownedNavigationBuilder.OwnsOne(child =>child.Data);});builder.Entity<Thing>().OwnsOne(thing =>thing.Data, ownedNavigationBilder =>{ownedNavigationBilder.ToJson();});base.OnModelCreating(builder);}publicDbSet<Thing>Things{get;set;}=null!;}publicclassThing{publicintThingId{get;set;}publicChildThing?Child{get;set;}publicDictionary<string,object>Data{get;set;}=null!;}publicclassChildThing{publicstringName{get;set;}=null!;publicDictionary<string,string>?Data{get;set;}}
The database which I created manually via SQL has one table Things with columns ThingId (identity, int), Data (nvarchar(4000)), Child(nvarchar(4000)).
In the database both dictionaries are serialized as {}, the name property of the child serializes properly. The Dictionary inside ChildThing is deserialized as empty dictionary which is to be expected considering the wrong serialization. Bizarrely the Dictionary that is directly inside Thing is deserialized as a dictionary with 1 element where the Key is "ThingId" and the value is whatever the id from the database is. Removing the Data or the Child property from Thing doesn't seem to affect the behavior of the other property.
Note from triage: validate that mapped property bags contain mapped properties, other than just auto-generated key properties, excluding join tables. This likely needs to be done for both Cosmos and relational, and for both JSON and table mapping.
I seem to be unable to serialize dictionaries in a JSON column with the new JSON support for SQL Server
The database which I created manually via SQL has one table Things with columns ThingId (identity, int), Data (nvarchar(4000)), Child(nvarchar(4000)).
In the database both dictionaries are serialized as {}, the name property of the child serializes properly. The Dictionary inside ChildThing is deserialized as empty dictionary which is to be expected considering the wrong serialization. Bizarrely the Dictionary that is directly inside Thing is deserialized as a dictionary with 1 element where the Key is "ThingId" and the value is whatever the id from the database is. Removing the Data or the Child property from Thing doesn't seem to affect the behavior of the other property.
The console output is
{"ThingId":0,"Child":{"Name":"Foo","Data":{"test":"2"}},"Data":{"test":"1"}}
Thing: 1,
Thing.Data:ThingId:1
Child.Name: Foo, Child.Data.Count: 0
EF Core version: 7
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 7.0
The text was updated successfully, but these errors were encountered: