我有一个包含玩家集合的实体名册。
public class Roster
{
public Roster()
{
Players = new List<Player>();
}
public int RosterId { get; set; }
[MaxLength(100)]
[Required]
public string RosterName { get; set; }
public ICollection<Player> Players { get; set; }
}
public class Player
{
public int PlayerId { get; set; }
[MaxLength(100)]
[Required]
public string PlayerName { get; set; }
public virtual ICollection<Roster> Rosters { get; set; }
}
我已经使用流利的 api 定义了关系
// many to many Roster - Players
modelBuilder.Entity<Roster>()
.HasMany(t => t.Players)
.WithMany(t=>t.Rosters)
.Map(m =>
{
m.ToTable("RosterPlayers");
m.MapLeftKey("RosterId");
m.MapRightKey("PlayerId");
});
我可以像这样将一名球员保存到名单中
var roster = rosterRepository.GetById(rosterId);
var player = playerRepository.GetById(playerId);
roster.Players.Add(player);
rosterRepository.Update(roster);
...
但是,当我检索花名册时,没有加载球员收藏。我验证了数据是否存在所有正确的值,并且以下 sql 确实产生了数据。
SELECT * from Rosters r
INNER JOIN RosterPlayers rp on r.RosterId = rp.RosterId
INNER JOIN Players p ON p.PlayerId = rp.PlayerId
我错过了什么?