Answer the question
In order to leave comments, you need to log in
How to set maximum level for navigational properties when using include?
Please prompt:
There is an entity question
public class Question
{
[Key]
public int Id { get; set; }
public string Info { get; set; }
public ICollection<Answer> Answers { get; set; }
}
public class Answer
{
[Key]
public int Id { get; set; }
public int QuestionId { get; set; }
[ForeignKey("QuestionId")]
public Question Question { get; set; }
public string Info { get; set; }
}
db.Questions.Include(x => x.Answers).ToList()
Answer the question
In order to leave comments, you need to log in
Googled the exact answer
https://github.com/aspnet/Mvc/issues/5910
in Startup.cs
services.AddMvc().AddJsonOptions(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
If you are trying to serialize an ef object to json, then JsonConvert will then recurse through all the fields inside and of course loop at some point. For such cases, they usually map the ef object to their model and already serialize it.
There is another option here
return JsonConvert.SerializeObject(results, Formatting.Indented,
new JsonSerializerSettings {
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question