C# Entity Framework returning broken JSon when using Include in DbSet C# Entity Framework returning broken JSon when using Include in DbSet json json

C# Entity Framework returning broken JSon when using Include in DbSet


If you are using Json.net you probably need to change the DefaultSettings to handle the serialization

JsonConvert.DefaultSettings = () => new JsonSerializerSettings{    PreserveReferencesHandling = PreserveReferencesHandling.All,    ReferenceLoopHandling = ReferenceLoopHandling.Serialize};

Or depending on your particular requirements

PreserveReferencesHandling

  • None : Do not preserve references when serializing types.
  • Objects : Preserve references when serializing into a JSON object structure.
  • Arrays : Preserve references when serializing into a JSON array structure.
  • All : Preserve references when serializing.

ReferenceLoopHandling

  • Error : Throw a JsonSerializationException when a loop is encountered.
  • Ignore : Ignore loop references and do not serialize.
  • Serialize :Serialize loop references.