title: ASP.NET Web API is showing objects in JSON that I didn't ask for
body: <p>I have a Web API with ASP.NET and I'm trying to return some data, but it's causing issues as it is referencing objects I don't want to be referenced.</p>
  
  <p>The class structure in this case is the following:</p>
  
  <p><strong>Entitats</strong> (Entities), <strong>Equips</strong> (Teams) and <strong>Esports</strong> (Sports)</p>
  
  <p>An <strong>Entity</strong> has many <strong>Teams</strong>, and one <strong>Team</strong> has just one <strong>Sport</strong>.</p>
  
  <p>I am using Entity Framework and the Objects have the relationships both ways; a <strong>Sport</strong> has many <strong>Teams</strong>, and a <strong>Team</strong> has an <strong>Entity</strong>.</p>
  
  <p>Here is the query I do to get an Entity with its Team, and each Team with its sport.</p>
  
  <pre><code> entitats _entitat = (
                     from e in db.entitats
                         .Include("equips.esports")
                     where e.id == id
                     select e
                 ).FirstOrDefault();
  </code></pre>
  
  <p>This should give me exactly what I want, the problem is that in the first Team, when it shows me the Sport, the Sport contains all other Teams (from this Entity) that have the same Sport, and then when it's time to show them on the Team array it uses $ref and $id.</p>
  
  <pre><code>"$id": "1",
  "equips": [
      {
          "$id": "2",
          "activitats_concedides": [],
          "activitats_demanades": [],
          "categories": null,
          "categories_competicio": null,
          "competicions": null,
          "entitats": {
              "$ref": "1"
          },
          "esports": {
              "$id": "3",
              // These shouldn't even be here
              "equips": [
                  {
                      "$ref": "2"
                  },
                  {
                      "$id": "4",
                      "activitats_concedides": [],
                      "activitats_demanades": [],
                      "categories": null,
                      "categories_competicio": null,
                      "competicions": null,
                      "entitats": {
                          "$ref": "1"
                      },
                      "esports": {
                          "$ref": "3"
                      },
                      "sexes": null,
                      "id": 8,
                      "nom": "Test 2",
                      "id_entitat": 1,
                      "id_categoria": 3,
                      "id_esport": 1,
                      "id_competicio": 2,
                      "id_categoria_competicio": null,
                      "id_sexe": 3,
                      "borrat": false
                  },
                  {
                      "$id": "5",
                      "activitats_concedides": [],
                      "activitats_demanades": [],
                      "categories": null,
                      "categories_competicio": null,
                      "competicions": null,
                      "entitats": {
                          "$ref": "1"
                      },
                      "esports": {
                          "$ref": "3"
                      },
                      "sexes": null,
                      "id": 9,
                      "nom": "Test 3",
                      "id_entitat": 1,
                      "id_categoria": 2,
                      "id_esport": 1,
                      "id_competicio": 2,
                      "id_categoria_competicio": null,
                      "id_sexe": 2,
                      "borrat": false
                  },
                  {
                      "$id": "6",
                      "activitats_concedides": [],
                      "activitats_demanades": [],
                      "categories": null,
                      "categories_competicio": null,
                      "competicions": null,
                      "entitats": {
                          "$ref": "1"
                      },
                      "esports": {
                          "$ref": "3"
                      },
                      "sexes": null,
                      "id": 10,
                      "nom": "prova",
                      "id_entitat": 1,
                      "id_categoria": 3,
                      "id_esport": 1,
                      "id_competicio": 2,
                      "id_categoria_competicio": null,
                      "id_sexe": 2,
                      "borrat": false
                  }
              ],
              "id": 1,
              "nom": "Futbol"
          },
          "sexes": null,
          "id": 3,
          "nom": "Test 1",
          "id_entitat": 1,
          "id_categoria": 6,
          "id_esport": 1,
          "id_competicio": 1,
          "id_categoria_competicio": null,
          "id_sexe": 1,
          "borrat": false
      },
      {
          "$ref": "4" // These should be the "full" objects
      },
      {
          "$ref": "5"
      },
      {
          "$ref": "6"
      }
  ],
  "telefons": [],
  "id": 1,
  "nom": "Futbol Club Sant Cugat del Valles",
  "direccio": "Sample Carrer 1",
  "cif": "B12345678",
  "temporada": "2019                ",
  "correu": "entitat1@test.com",
  "facebook": null,
  "instagram": null,
  "twitter": null,
  "password": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92",
  "borrat": true}
  </code></pre>
  
  <p>I didn't ask the Teams of each Sport, so I don't know why it's showing them. The same happens with the Teams' Entity, in this case, it's not an issue because it doesn't mess with the output, but in other cases it is. I suppose what it does is that if it shows you objects you "asked" as close to the top as possible, and then in the place you actually need them, it just references.</p>
  
  <p>If you guys know what's wrong I would really appreciate it. Thanks!</p>
  
  <p>PD: I've tried changing this option without success, it just makes things worse.</p>
  
  <pre><code>var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
  json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
  </code></pre>