【问题标题】:C# Backend Pro Is needed ! Data weirdness需要 C# 后端专业版!数据怪异
【发布时间】:2015-12-08 07:49:54
【问题描述】:

这是我的代码

  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;               
        }
    }

我正在使用 API,由于序列化问题,我需要将我的实体转换为 DTOobject。

这是来自 Dto 转换器的 sn-p

 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;
    }

这是 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
  }
}

我的数据库包含 3 个玩家 - 笔记转换为 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
      }
    ]
  }
]

所以我真的无法弄清楚我的问题。为什么我的 DtoPlayer.Name 与 DtoGroup.Name 同名。看看我的包含。

【问题讨论】:

  • t.Groups 来自哪里?请显示所有适用的代码。否则无法提供帮助。
  • 您在查询中使用了“包含”。看看这个回复。 stackoverflow.com/questions/794283/…
  • Read方法返回的tournament对象中的数据是否OK?
  • 差不多了。我的 dtoplayers.name 似乎与 dtogroup.name 同名。所以它不能正确加载我的玩家实体。有什么想法吗?

标签: c# entity-framework linq asp.net-web-api


【解决方案1】:

抛出此异常的原因是您尝试使用 您已释放数据库上下文后未从数据库中检索到的属性。您可以在调试时看到数据,因为变量是在 using 范围内定义的。您还应该在 Group 类上获取 Teams

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;               
}

【讨论】:

  • 会尽快试用并回复您
  • 成功了,团队错误为空。添加了另一个包含,这样我就可以让球员进入球队。现在我们有我用了无数个小时来修复的错误 - 线程更新
  • @KlausJakobsen 你检查过你的数据了吗?您的代码看起来不错。
  • Ufuk,是的,检查了三次。如果我更改数据库中的组名,它也会将 json 中的 dtoplayer.name 更改为相同的名称。 - 我的数据库中只有 3 名玩家,他们不被称为 groupONE :o)
  • 刚刚更新了我的初始化程序中的数据并更新了我从我的 Api 获得的 json。看看 dto.team 里面的 dto.player.name 和 dto.group.name 是一样的——你能理解这个吗?谢谢你帮忙
【解决方案2】:

既然你说你可以在调试模式下看到数据,试试这个方法。

  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);

【讨论】:

  • 对不起队友 - .ToList() 没有做到这一点。它和以前一样失败。
  • 这意味着 Include 不起作用。摆脱它并改用join
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-16
  • 1970-01-01
  • 2020-04-06
相关资源
最近更新 更多