-
Notifications
You must be signed in to change notification settings - Fork 236
Migrations blocked by: The type mapping for 'JsonElement' has not implemented code literal generation #3107
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
The issue seems to be related to this commit. The I've opened a PR with the implementation for this method, taken from the original mapper |
This is a significant regression that seriously affects the ability to use JsonElement. It warrants a more thorough investigation to understand the full impact and find a viable solution. |
tl;dr to work around this in 8.0, configure the new jsonb column with an explicit default value as follows: protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>().Property(b => b.JsonElement).HasDefaultValueSql("{}");
} Alternatively, using JsonDocument instead of JsonElement should make the problems go away. Note that adding a JsonElement property and generating a migration never properly worked (so I'm not sure there's an actual regression here - @Laurianti some details would be good). Using 7.0, the generated migration was: migrationBuilder.AddColumn<JsonElement>(
name: "JsonElement",
table: "Blogs",
type: "jsonb",
nullable: false,
defaultValue: System.Text.Json.JsonDocument.Parse("", new System.Text.Json.JsonDocumentOptions()).RootElement); Note the invalid empty string argument to Parse; when trying to actually generate the SQL for this migration, one gets:
The workaround above with HasDefaultValueSql should take care of that problem as well. Here's what's going on with JsonElement:
|
@roji thank you, i can confirm that setting the default value avoids this bug and it might work for us in the meantime. We cannot use the JsonDocument since it's an IDisposable and it would be a real nightmare to manage correctly in our scenario. |
@luca-esse if my understanding is correct, not disposing a JsonDocument only results in memory not being pooled (and therefore increased GC pressure) - there's no leak or any effect that's more serious (see docs). So AFAIK, reading JsonDocument should be OK. |
@roji you are right, I've missed that part. But still, not ideal since it would trigger missing disposal warnings from code analyzers. |
I seem to be hitting this issue moving away from a |
If Anyone has a Problem using the Workaround above with a DefaultValue on a JsonElement? (mapped to jsonb) |
Versions:
EF 8.0.2
Npgsql.EntityFrameworkCore.PostgreSQL 8.0.2
Microsoft.EntityFrameworkCore.Tools 8.0.2
We are facing an issue when try to add/rename/remove or change nullability of JsonElement fields after the first migration.
We are only able to generate an initial migration with JsonElements, after that, the add migration command fails with the following error:
Steps to reproduce:
add-migration Initial
add-migration MySecondMigration
The text was updated successfully, but these errors were encountered: