gpt4 book ai didi

c# - 如何将行按特定顺序保存到 ASP .NET 中的关联实体中?

转载 作者:行者123 更新时间:2023-11-29 22:31:07 27 4
gpt4 key购买 nike

我正在创建一个可以注册玩游戏的玩家的系统。一场游戏最多可以有4名玩家,玩家可以无限次玩游戏。相关源码:

    public class Player
{
public int PlayerId { get; set; }

[Required]
public string Username { get; set; }

public virtual ICollection<Game> Games { get; set; }
}

并且:

 public class Game
{
public int GameId { get; set; }

[Required]
public string Location { get; set; }

public virtual ICollection<Player> Players { get; set; }
}

EF 正确生成了这些表,并且还生成了一个额外的表:PlayerGames,它有两列:Player_PlayerId 和 Game_GameId。它看起来像这样:http://imgur.com/AGdKN3P

GamesController 的 Create 方法如下所示:

   public ActionResult Create([Bind(Include = "GameId,Location,PlayerId")] Game game)
{
int[] ids = Request.Form["PlayerId"].Split(',').Select(Int32.Parse).ToArray();
List<Player> k = ids.Select(t => db.Players.First(x => x.PlayerId == t)).ToList();
ICollection<Player> players = ids.Select(t => db.Players.First(x => x.PlayerId == t)).ToList();
game.Players = players;

db.Database.Log = s => Debug.Write(s);
if (ModelState.IsValid)
{
db.Games.Add(game);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(game);
}

问题在于 db.SaveChanges() 生成的查询忽略了玩家列表的顺序:Id-s 每次都会按顺序排列。然而,对于系统来说,了解玩家在列表中的顺序非常重要。有什么方法可以将其插入数据库并保留顺序吗?

以下是SQL日志供引用:

INSERT [dbo].[PlayerGames]([Player_PlayerId], [Game_GameId])
VALUES (@0, @1)
-- @0: '2' (Type = Int32)
-- @1: '1013' (Type = Int32)
-- Executing at 4/21/2015 1:22:13 PM +02:00
-- Completed in 1 ms with result: 1

INSERT [dbo].[PlayerGames]([Player_PlayerId], [Game_GameId])
VALUES (@0, @1)
-- @0: '3' (Type = Int32)
-- @1: '1013' (Type = Int32)

-- Executing at 4/21/2015 1:22:13 PM +02:00
-- Completed in 0 ms with result: 1

INSERT [dbo].[PlayerGames]([Player_PlayerId], [Game_GameId])
VALUES (@0, @1)
-- @0: '4' (Type = Int32)
-- @1: '1013' (Type = Int32)
-- Executing at 4/21/2015 1:22:13 PM +02:00
-- Completed in 0 ms with result: 1

INSERT [dbo].[PlayerGames]([Player_PlayerId], [Game_GameId])
VALUES (@0, @1)
-- @0: '6' (Type = Int32)
-- @1: '1013' (Type = Int32)
-- Executing at 4/21/2015 1:22:13 PM +02:00
-- Completed in 0 ms with result: 1

最佳答案

你的类应该是这样的:

public class Player
{
public int PlayerId { get; set; }
[Required]
public string Username { get; set; }
public virtual ICollection<OpenedGame> OpenedGames { get; set; }
}

public class Game
{
public int GameId { get; set; }
[Required]
public string Location { get; set; }
public virtual ICollection<OpenedGame> OpenedGames { get; set; }
}

public class OpenedGame
{
public int OpenedGameId { get; set; }
public int GameId { get; set; }
public virtual Game Game { get; set; }
public int PlayerId { get; set; }
public virtual Player Player { get; set; }
public int Location { get; set; }
}

这样您就可以跟踪哪个玩家在游戏中的哪个位置。

编辑:如果您想检查玩家正在玩什么游戏,请执行以下操作:

var list = context.OpenedGames.Where(x=>x.PlayerId==pId).ToList();

或玩单一游戏的玩家列表:

var list = context.OpenedGames.Where(x=>x.GameId==gId).ToList();

关于c# - 如何将行按特定顺序保存到 ASP .NET 中的关联实体中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29771291/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com