Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Errors when Serializing to JSON #4160
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
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.
@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).
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!).
referenced this issue
Oct 5, 2016
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 !!!
added a commit
Dec 17, 2017
referenced this issue
Apr 10, 2018
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