1

Here's my Code

  public Tournament Read(int id)
    {
        using (var context = new DragonLairContext())
        {
            Tournament tournament = context.Tournaments
                 .Include(a => a.Game)
                    .Include(g => g.Game.Genre)
                    .Include(b => b.Groups.Select(g => g.Teams))
                    .Include(k => k.Groups.Select(y => y.Teams.Select(l => l.Players)))
                    .Include(c => c.TournamentType)
                    .FirstOrDefault(d => d.Id == id);
            return tournament;               
        }
    }

I'm using API and due to serialization issues, I need to convert my entities to DTOobject.

Here's a snippet from the Dto Converter

 public override DTOTournament Convert(Tournament t)
    {
        if (t.Game == null || t.TournamentType == null || t.Groups == null) throw new ArgumentException("Missing some data");
        DTOTournament dtoTournament = new DTOTournament();
        List<DTOGroup> dtoGroups = new List<DTOGroup>();

        DTOTournamentType dtoTournamentType = new DTOTournamentType() { Id = t.TournamentType.Id, Type = t.TournamentType.Type };


        foreach (var group in t.Groups)
        {
            if (group.Teams == null) return dtoTournament;
            List<DTOTeam> dtoTeams = new List<DTOTeam>();
            foreach (var team in group.Teams)
            {
                List<DTOPlayer> dtoPlayers = new List<DTOPlayer>();
                if (team.Players == null) return dtoTournament;
                foreach (var player in team.Players)
                {
                    dtoPlayers.Add(new DTOPlayer() { Id = player.Id, Name = player.Name });
                }
                dtoTeams.Add(new DTOTeam()
                {
                    Id = team.Id,
                    Name = team.Name,
                    Win = team.Win,
                    Loss = team.Loss,
                    Draw = team.Draw,
                    DtoPlayers = dtoPlayers
                });
            }
            dtoGroups.Add(new DTOGroup()
            {
                Id = group.Id,
                Name = group.Name,
                DtoTeams = dtoTeams,
                DtoTournament = dtoTournament
            });
        }

        dtoTournament.DTOTournamentType = dtoTournamentType;
        dtoTournament.Id = t.Id;
        dtoTournament.Name = t.Name;
        dtoTournament.StartDate = t.StartDate;
        dtoTournament.DtoGroups = dtoGroups;
        dtoTournament.DtoGame = new DTOGame() { Id = t.Game.Id, Name = t.Game.Name, DtoGenre = new DTOGenre() { Id = t.Game.Genre.Id, Name = t.Game.Genre.Name } };
        return dtoTournament;
    }

Here's the Json

     {
  "$id": "1",
  "Id": 1,
  "Name": "I'm a Tournament",
  "StartDate": "2015-12-08T00:00:00",
  "DTOTournamentType": {
    "$id": "2",
    "Id": 1,
    "Type": "I'm type 2vs2",
    "DtoTournaments": null
  },
  "DtoGroups": [
    {
      "$id": "3",
      "Id": 1,
      "Name": "I'm a Group",
      "DtoTournament": {
        "$ref": "1"
      },
      "DtoTeams": [
        {
          "$id": "4",
          "Id": 1,
          "Draw": 0,
          "Loss": 0,
          "Win": 0,
          "Name": "I'm a Team",
          "DtoPlayers": [
            {
              "$id": "5",
              "Id": 1,
              "Name": "I'm a Group",
              "DtoTeams": null
            }
          ],
          "DtoGroups": null
        },
        {
          "$id": "6",
          "Id": 2,
          "Draw": 0,
          "Loss": 0,
          "Win": 0,
          "Name": "I'm a Team",
          "DtoPlayers": [
            {
              "$id": "7",
              "Id": 1,
              "Name": "I'm a Group",
              "DtoTeams": null
            }
          ],
          "DtoGroups": null
        }
      ]
    }
  ],
  "DtoGame": {
    "$id": "8",
    "Id": 1,
    "Name": "Im a Game - Wars",
    "DtoGenre": {
      "$id": "9",
      "Id": 1,
      "Name": "I'm Genre Roleplaying",
      "DtoGames": null
    },
    "DtoTournaments": null
  }
}

My DB contains 3 players - note converted to json

      [
  {
    "$id": "1",
    "Id": 1,
    "Name": "I'm player Søren",
    "DtoTeams": [
      {
        "$id": "2",
        "Id": 1,
        "Draw": 0,
        "Loss": 0,
        "Win": 0,
        "Name": "I'm a Team",
        "DtoPlayers": null,
        "DtoGroups": null
      }
    ]
  },
  {
    "$id": "3",
    "Id": 2,
    "Name": "I'm player Mark",
    "DtoTeams": [
      {
        "$id": "4",
        "Id": 1,
        "Draw": 0,
        "Loss": 0,
        "Win": 0,
        "Name": "I'm a Team",
        "DtoPlayers": null,
        "DtoGroups": null
      }
    ]
  },
  {
    "$id": "5",
    "Id": 3,
    "Name": "I'm player René",
    "DtoTeams": [
      {
        "$id": "6",
        "Id": 2,
        "Draw": 0,
        "Loss": 0,
        "Win": 0,
        "Name": "I'm a Team",
        "DtoPlayers": null,
        "DtoGroups": null
      }
    ]
  }
]

So my problem which I really can't figure out. How come my DtoPlayer.Name has the same name as DtoGroup.Name. Have a look at my includes.

Klaus Jakobsen
  • 215
  • 2
  • 9
  • Where does `t.Groups` come from? Please show all applicable codes. Otherwise it's not possible to help. – Kosala W Dec 08 '15 at 07:54
  • You are using Include in your query. Have a look at this reply. http://stackoverflow.com/questions/794283/linq-to-entities-include-method-not-loading – Kosala W Dec 08 '15 at 07:59
  • Are the data OK in the `tournament` object returned from the `Read` method? – Gert Arnold Dec 08 '15 at 18:21
  • Almost gert. My dtoplayers.name seems to have the same name as dtogroup.name. So it doesn't load my player entity correctly. Any ideas? – Klaus Jakobsen Dec 08 '15 at 19:22

2 Answers2

2

This exception is thrown because you are trying to use a property that was not retrieved from the database after you have disposed the database context. You can see the data while debugging because the variable is defined inside the using scope. You should also fetch the Teams on Group class.

using (var context = new DragonLairContext())
{
    Tournament tournament = context.Tournaments 
            .Include(a => a.Game)
            .Include(g => g.Game.Genre)
            .Include(b => b.Groups.Select(g => g.Teams))
            .Include(c => c.TournamentType)
            .FirstOrDefault(d => d.Id == id);

    return tournament;               
}
Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
  • Will try it out asap and get back to you – Klaus Jakobsen Dec 08 '15 at 08:12
  • It worked the the team error was null. added another include so i could get the players inside the teams. Now we have the error I have used countless hours to fix - thread updated – Klaus Jakobsen Dec 08 '15 at 08:28
  • @KlausJakobsen Have you checked your data? Your code looks fine. – Ufuk Hacıoğulları Dec 08 '15 at 08:32
  • Ufuk, yup tripple checked. If i change the group name in my db, it will also change the dtoplayer.name in the json to the same name. - only have 3 players in my db and they are not called groupONE :o) – Klaus Jakobsen Dec 08 '15 at 08:36
  • Just updated the data in my initializer and updated the json from I get from my Api. Look at dto.player.name inside the dto.team is is the same as dto.group.name - can you make any sense of this? thanks for helping out btw – Klaus Jakobsen Dec 08 '15 at 09:40
0

Since you said that you could see data in debug mode, try this method.

  Tournament tournament = context.Tournaments
            .Include(a => a.Game)
            .Include(g => g.Game.Genre)
            .Include(b => b.Groups)
            .Include(c => c.TournamentType).ToList()
            .FirstOrDefault(d => d.Id == id);
Kosala W
  • 2,133
  • 1
  • 15
  • 20