-
Notifications
You must be signed in to change notification settings - Fork 228
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
List Of List to JsonB .Net 8 #3172
Comments
@zN3utr4l which exact version of EF and EFCore.PG are you using? Can you make sure that it's the latest patch version for both? If you're still getting the error, please post a minimal, runnable console program that shows the error happening - I can investigate at that point. |
Minimal Repo: https://github.com/zN3utr4l/ErrorNpgsql-ListOfLists-Jsonb/tree/main Yes, I'm using the latest version, specifically these are the dependencies:
|
I ran your code, accessed the endpoint (/error-jsonb) and got an empty array, as expected (the database is empty). I then added the following to the handler: context.Add(new MyEntity
{
JsonbFields = [
[new() { Key = "key1", Value = "value1"}],
[new() { Key = "key2", Value = "value2"}]
]
});
context.SaveChanges(); After accessing the endpoint again, I get the expected output: [{"id":1,"jsonbFields":[[{"key":"key1","value":"value1"}],[{"key":"key2","value":"value2"}]]}] It's very likely to you have some data in your database that's somehow malformed and causing this; it's possible the EF's JSON deserializer (which is used when you map via owned entities) is stricter in some way, which that data used to work but doesn't any more. Long story short, you'll have to track down the offending column data. If it looks like correct JSON, please post it here so I can investigate. |
This is what comes back if it adds what you did.
Sorry, in DB it looks like in the photo, but you're right, the EF mapping comes back to me correctly: Why isn't it written in plain text in DB? |
I changed the endpoint like this, to make the list of lists look like the json I sent initially, but now the entity I inserted before seems to no longer be serialized.
Every time I rerun the program, the entity inserted in the last execution is no longer mapped and is empty as in the json sent above |
@zN3utr4l it really would be easiest if you could submit a simple console program - just a single page of code - which shows the exception happening. I'm spending a lot of time trying to understand all the snippets and screenshots above. |
I put the ErrorNpgsqlJsonb project (https://github.com/zN3utr4l/ErrorNpgsql-ListOfLists-Jsonb), which is a console app, all in one file, hope it's fine now. run the app console a first time and it should print the entities it created, but if you relaunch the app console a second time, you will see how the old created entities are not printed, they appear to be of type: |
I've taken another look, and I can't see the original error you reported above ( In any case, regarding the ToString() printing /cc @ajcvickers for this possibly interesting "inconsistent" behavior; but to me everything seems like it's working as expected. Simplified code sample, with a single run |
Above I inserted the screen of PGAdmin with what is inserted in the JsonbFields field, I don't think it's correct, it is inserting the information of the first nested list i think, and not the content of the json.
This is the plain text that is in db, it's not an object, it's exactly how you read it. From this it seems clear to me that something is wrong. This means that EF is writing to db wrong. In fact, when I go to get the entities I get an empty object back, even though I had inserted the JsonbFields correctly |
@roji What you describe is by-design. |
Sorry for insisting but I don't understand if you don't consider this error a bug. As I understand it, @roji doesn't consider it a bug, but in my opinion it's because, as I have repeated many times, you have to look at the db with PgAdmin, if you look at what it saves in the JsonBFields field you would immediately notice the problem. |
@zN3utr4l you originally reported an error with In this comment of yours you asked a completely unrelated question regarding the ToString() behavior when using Castles.Proxies - both @ajcvickers and I consider this to be by design (no bug). Now, if you want us to look into the original bug ( |
ToString() when using Castles.Proxies was a question, not a bug report.
But did you check this after running my repro? The error Invalid token type: 'StartArray'.; it is generated by the fact that the Lists of List in the jsonb field are not saved correctly and some middleware that I had in the app (not in the repro) was unable to serialize. look, let's do something, I'll open another issue where I don't refer to Invalid token type: 'StartArray'. But only to the fact that it is not saved correctly. |
From the above it sounds like you have some invalid JSON in your database, which wasn't generated by EF; if that's the case, then EF is behaving correctly. If you believe EF itself is the one generating invalid JSON in SaveChanges, please submit a repro to show that. |
@zN3utr4l thanks, I finally managed to "Capacity: 4" in the database, which is definitely a bug; I'm pretty sure that A couple comments on the above issue... First, see dotnet/efcore#33913 for what a minimal repro looks like - it's a console program, with just the lines needed to actually reproduce the issue; the repros you've been providing consist of projects with many files, needlessly using ASP.NET (rather than a simple console program), etc. That's fine, but open source maintainers (like myself) need to go through dozens of issues each day, and the more minimal/tight the repo, the more you're helping us understand what's going on. In addition, this issue started with a non-reproducible "Invalid token type: 'StartArray'", continued to an unrelated question on the ToString() representation with Castle.Proxies, and finally ended up with the actual issue. Again, that's absolutely OK - but it doesn't help me remember what this issue is about when I revisit it and re-read it from the top. I advise making an effort to keep issues very focused, dealing with a single problem, and containing a small, completely clear code repro; that saves everyone's time and helps maintainers manage their repos. |
I'm migrating a .net 6 project to .net 8.
I have this entities:
in .net 6 by setting
HasColumnType('jsonb')
everything is ok, now with .net 8 (https://www.npgsql.org/efcore/mapping/json.html#tojson-owned-entity-mapping) doing this:This error comes up:
System.InvalidOperationException: Invalid token type: 'StartArray'.
This is what the contents of the jsonb column look like when created with .net 6:
The text was updated successfully, but these errors were encountered: