-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Errors when Serializing to JSON #4160
Comments
This appears to be an issue with Entity Framework. |
It's not entirely an EF issue. Yes, there is a circular reference issue in EF, but if I do |
I had something similar, try setting this in the
|
@Eilon shouldn't the default needs to be changed? |
I think the default value of not ignoring reference loops is correct because you want to avoid serializing things that have loops to begin with. Changing the default would likely mask problems with the data being serialized, no? |
Erroring when a loop is encountered is standard behavour for serializers. Options are there to fix the error (including updating the serialized object). I don't think you should pick one for devs. |
@Eilon: I find changing of default for the best way. The usage of @JamesNK : I could agree with you if the initial model were oriented on serialization. The developer want just include some field which could be get by INNER JOIN and so he uses Thus, in my personal opinion, changing of defaults would be make Entity Framework more user friendly, especially for newcomers and especially if one unable to report the error with the exact reason on the problem. |
@Eilon I agree that if you pass an entity with reference loop to a view you're doing something wrong because you're not using ViewModel/DTO. |
Using view models and DTOs is definitely recommended. It's really not a goal for MVC to allow general serialization of EF data models. The reality is that there's effectively a bug in the app, and the bug needs to be fixed, not ignored. Completely ignoring reference loops is a Very Bad IdeaTM because it just means the serialized data is missing stuff and you can't predict where. That's not good. |
@Eilon I agree, but the scaffolding and templates should be adjusted to use DTOs (AutoMapper is probably a good idea). |
@gdoron issues with the templates can be logged here: https://github.com/aspnet/Templates/ However, one thing that's been a constant challenge for us when making the templates is balancing out how easy it is to understand vs. how "real world" something is. Using view models, binding models, and serialization models is definitely more powerful, but it's a large maintenance cost for an app, and more difficult for someone starting to understand (e.g. "how come when I add/change a property in my model I have to do it in 4 places??"). Regarding the Bad Gateway error that's a really tough one: the problem is that you can't know about the error until it's too late, unless you buffer the entire response (which could be several GB or more!). |
The fix (ignore loops) worked great for me my scenario: Web API with EF Core - Returning child data along with parent (via Include)
Ran into the issue described above. Didn't get an error, just no response from the web api. Happened to stumble upon this thread. Added the fix (ignore loops). Worked like a champ !!! |
aspnet/Mvc#4160 for more info
I agree with @Eilon, but this bug is still unresolved as to date, and two years have passed. |
@gdoron solution is a bad practice because we'd rather avoid circular dependency. I just want my object and its children propreties, simple as that. Ignoring the circular loop and adding the option in the startup.cs file is not a solution at all, it's just a workaround.. I'm kinda shocked because i'm learning the framework and stumbling upon critical bugs such as this one. As @alexlomba87 noticed, the bug is present and ignored in most .NET Core tutorials and documentation pages. I just lost one entire day at work trying to fix this 👎 and there is no solution |
referencing problem |
I am hitting an error that I can't quite pinpoint. If you can tell me how to debug it better I'll gladly provide the details. This was working fine with EF7 before the change to EntityFrameworkCore 1.0, but now I'm getting these two types of errors. I don't think it's an EF issue because the data gets returned from the database fine, it seems to error when it gets serialized in the WebApi. Although maybe it's an issue with Json.Net.
I have an EF model of clients that have projects. A project has a Project Manager, a Project Type, (etc) as related entities.
When I query the API endpoint to list the projects, I run this code:
I get the error:
If I remove the line
.Include(p => p.Client)
then I get a different issue: the json gets truncated after about 259 projects. If I put a.Take(258)
in there, then it works fine.It looks like 2 different issues - the CGI error and the truncating error. As mentioned, this worked a few days ago before I upgraded to AspNetCore and EntityFrameworkCore.
I'm not sure how to get more error information out but if direct me I'll do it.
The text was updated successfully, but these errors were encountered: