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'm in the process of upgrading an application from EF Core 8 to EF Core 9, but I'm encountering a change in how JSON is being escaped.
I have an entity with my own LocalizableString type that I'm storing as JSON by using ToJson() in the model configuration. For an existing row, if I attempt to update a property on LocalizableString with a non-ASCII character, then it seems like the value is now escaped twice. When reading the value back from the database with EF, the value appears different than what EF stored.
Include your code
Here is code to reproduce the problem:
usingMicrosoft.EntityFrameworkCore;varoptions=newDbContextOptionsBuilder<MyDbContext>().UseSqlServer("Server=.;Database=EF9JsonEscapeRegression;Trusted_Connection=True;Encrypt=False").Options;awaitusingvarctx=newMyDbContext(options);awaitctx.Database.EnsureDeletedAsync();awaitctx.Database.EnsureCreatedAsync();varname=newLocalizableString{En="Door"};varitem=newItem{Name=name};ctx.Items.Add(item);awaitctx.SaveChangesAsync();item.Name.No="Dør";// Here is the crucial partawaitctx.SaveChangesAsync();awaitusingvarctx2=newMyDbContext(options);varactualItem=awaitctx2.Items.SingleAsync();// EF 8.0.11: "Dør"// EF 9.0.0: "D\\u00F8r"Console.WriteLine(actualItem.Name.No);publicclassMyDbContext(DbContextOptions<MyDbContext>options):DbContext(options){publicDbSet<Item>Items{get;init;}protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){modelBuilder.Entity<Item>().OwnsOne(x =>x.Name, b =>b.ToJson());}}publicclassItem{publicintId{get;init;}publicrequiredLocalizableStringName{get;set;}}publicclassLocalizableString{publicrequiredstringEn{get;set;}publicstring?No{get;set;}}
The generated update query is slightly different depending on the EF version.
Here is the query for EF 8:
exec sp_executesql N'SET IMPLICIT_TRANSACTIONS OFF;
SET NOCOUNT ON;
UPDATE [Items] SET [Name] = JSON_MODIFY([Name], ''strict $.No'', @p0)
OUTPUT 1
WHERE [Id] = @p1;
',N'@p0 nvarchar(4000),@p1 int',@p0=N'Dør',@p1=1
Here is the query for EF 9:
exec sp_executesql N'SET IMPLICIT_TRANSACTIONS OFF;
SET NOCOUNT ON;
UPDATE [Items] SET [Name] = JSON_MODIFY([Name], ''strict $.No'', @p0)
OUTPUT 1
WHERE [Id] = @p1;
',N'@p0 nvarchar(4000),@p1 int',@p0=N'D\u00F8r',@p1=1
It seems to only be a problem when updating a property on the JSON serialized type. Replacing the instance works as expected.
Include provider and version information
EF Core version: 9.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 9.0
Operating system: Windows 11
IDE: Visual Studio Professional 2022 17.12
The text was updated successfully, but these errors were encountered:
#30744 is tracking the work to add global customization options for json reader/writer, which includes the encoder
For now, you can also try to replace JsonReaderWriter with a custom implementation which would unescape string when it's reading it from the json reader.
We only have metadata API for this at the moment - it's called SetJsonValueReaderWriterType. You would need to copy the implementation of current JsonStringReaderWriter and change FromJsonTyped to something like:
But keep in mind, Utf8JsonWriter is escaping everything for security reasons, so make sure you are not exposing your app to some problems, e.g. if the inputs are coming from untrusted source.
I'm in the process of upgrading an application from EF Core 8 to EF Core 9, but I'm encountering a change in how JSON is being escaped.
I have an entity with my own
LocalizableString
type that I'm storing as JSON by usingToJson()
in the model configuration. For an existing row, if I attempt to update a property onLocalizableString
with a non-ASCII character, then it seems like the value is now escaped twice. When reading the value back from the database with EF, the value appears different than what EF stored.Include your code
Here is code to reproduce the problem:
The generated update query is slightly different depending on the EF version.
Here is the query for EF 8:
Here is the query for EF 9:
It seems to only be a problem when updating a property on the JSON serialized type. Replacing the instance works as expected.
Include provider and version information
EF Core version: 9.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 9.0
Operating system: Windows 11
IDE: Visual Studio Professional 2022 17.12
The text was updated successfully, but these errors were encountered: