-
Notifications
You must be signed in to change notification settings - Fork 236
No coercion operator is defined between types 'System.IO.MemoryStream' and 'System.Nullable`1[System.Text.Json.JsonElement]' #2977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I tried all Also tried changing
|
@taspeotis I think I'm going to need a minimal repro for this - can you please post one? Also, did this work for you in 7.0, and regress in 8.0? |
I'm running in the same issue. I have a minimal repro here for you. I have my postgres up and running on localhost:5432 with user posgres.. My password is in environment variable PG_PASSWD. I created a database with name npgsql_jsonelement_issue_repro for testing. |
@roji Yes it works in 7.x @UliPlabst Thanks for that, I have my own version as well using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Npgsql;
var dataSourceBuilder = new NpgsqlDataSourceBuilder
{
ConnectionStringBuilder =
{
Host = "localhost",
Database = "npgsql",
Username = "postgres",
Password = "postgres"
}
};
// JSON all the things...
dataSourceBuilder.EnableDynamicJson();
dataSourceBuilder.EnableRecordsAsTuples();
dataSourceBuilder.EnableUnmappedTypes();
await using var dataSource = dataSourceBuilder.Build();
var dbContextOptionsBuilder = new DbContextOptionsBuilder();
dbContextOptionsBuilder.UseNpgsql(dataSource);
await using var context = new NpgsqlContext(dbContextOptionsBuilder.Options);
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
await context.Database.ExecuteSqlRawAsync(
"""INSERT INTO "EntitiesWithNullableJsonElement" ("Id", "Json") VALUES (8, NULL);""");
await context.EntitiesWithNullableJsonElement.ToListAsync();
public sealed class NpgsqlContext(DbContextOptions options) : DbContext(options)
{
public DbSet<EntityWithNullableJsonElement> EntitiesWithNullableJsonElement { get; set; }
}
public sealed class EntityWithNullableJsonElement
{
public int Id { get; set; }
public JsonElement? Json { get; set; }
} |
Here's a repro for the second error where I tried to change using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Npgsql;
await using var context = new NpgsqlContext();
public sealed class NpgsqlContext : DbContext
{
public DbSet<EntityWithNullableJsonElement> EntitiesWithNullableJsonElement { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
var dataSourceBuilder = new NpgsqlDataSourceBuilder
{
ConnectionStringBuilder =
{
Host = "localhost",
Database = "npgsql",
Username = "postgres",
Password = "postgres"
}
};
// JSON all the things...
dataSourceBuilder.EnableDynamicJson();
dataSourceBuilder.EnableRecordsAsTuples();
dataSourceBuilder.EnableUnmappedTypes();
var dataSource = dataSourceBuilder.Build();
optionsBuilder.UseNpgsql(dataSource);
}
}
public sealed class EntityWithNullableJsonElement
{
public int Id { get; set; }
public JsonElement? Json { get; set; }
}
public sealed class EntityWithNullableJsonElement
{
public int Id { get; set; }
+ public JsonElement Json { get; set; }
- public JsonElement? Json { get; set; }
}
You'll get
|
I should clarify that the migration issue: I'm unsure if it works in Npgsql 7, I just stumbled upon it while trying to change the model to work around whatever has Npgsql 8 unhappy with But the initial issue, yes, Npgsql 7 handles it fine. |
In my case I'm sure that it worked in Npgsql 7. |
Thank you... It'll take me a few days to get around to it but I'll investigate. |
Seeing the same problem after upgrading a project from npgsql 7 to 8 and trying to read a JsonElement? field. Changing the type of the field from JsonElement? to JsonElement makes the problem go away. Stack trace for reference:
|
Getting the same error. Also looks like it's working fine on 8.0.0-rc.2 and only breaks on 8.0.0 |
I am probably completely wrong but I feel like |
I just tried to apply a workaround based on @taspeotis suggestion of the underlying issue. I can now confirm that applying a custom IPropertyAddedConvention fixed the issue in my reproduction repository. Here's the code public class FixNpgsqlJsonElementHackConvention : IPropertyAddedConvention
{
private NpgsqlJsonTypeMapping? _jsonTypeMapping;
public void ProcessPropertyAdded(IConventionPropertyBuilder propertyBuilder, IConventionContext<IConventionPropertyBuilder> context)
{
var property = propertyBuilder.Metadata;
if (property.ClrType == typeof(JsonElement?) && property.GetColumnType() is null)
{
property.SetTypeMapping(_jsonTypeMapping ??= new NpgsqlJsonTypeMapping("jsonb", typeof(JsonElement?)));
}
}
}
//and in DbContext
public class AppDbContext: DbContext
{
//... omitted ....
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder.Conventions.Add(_ => new FixNpgsqlJsonElementHackConvention());
}
} So as it appears with this workaround the issue is no longer blocking us from moving to .net8, hurray! I will merge the .net8 branch of my project tomorrow and see if the workaround works there as well but I am optimistic. |
Fyi I confirmed today, the workaround is working for my project |
Does anyone know if this might be causing this error (I'm using the workaround) and if theres any (simpleish) way around it? I'm trying to query:
Where
|
The workaround provided by UliPlabst works if you select entity as is but it still fails if you map it to DTO. I have the following entity:
With workaround it works when I query the whole entity with
But it worked for 7.x and 8.0.0-rc.2. I've also tried to remove |
Just ran into this in 8.0.5 (JsonElement? as a property - backed by a nullable jsonb colum - not supported), would be good to see PR #3008 merged |
Also seeing this in 8.0.5 when it previously worked in 7.x versions. Would be great to see #3008 merged. |
I got the same error, but when using SQL Server. A workaround was to add a converter:
not the efficient way, but it works. In my model I have this property:
|
Everyone, apologies for leaving this uninvestigated for so long... @UliPlabst's suggestion indeed turned out to be correct, I'll fix this for the next 8.0.x patch version. @Misiu it makes sense for this issue to exist in the SQL Server provider, it would need a workaround hack similar to what Npgsql has, until dotnet/efcore#32192 is fixed. |
I've upgraded to Npgsql 8 and started getting this:
In
Program.cs
I have added:But the error persists. I'll try and do a minimal reproducible example, but for now I believe it's this POCO causing grief:
Npgsql 7.x does not have this error.
The text was updated successfully, but these errors were encountered: