0

我的问题的标题很简单。我想我真正遇到的问题是了解我的对象如何相互关联以及这些关系如何存储在数据库中。

我有一个团队课程:

public class Team
{
    public virtual int ID { get; set; }
    public virtual string Name { get; set; }
    public virtual IEnumerable<Player> Players { get; set; }
} 

还有一个 Player 类

public class Player
{
    public virtual int ID { get; set; }
    public virtual string Name { get; set; }
    public virtual int Number { get; set; }
    public virtual string Position { get; set; }
    public virtual Team Team { get; set; }
}

我的数据源接口

public interface ITeamDataSource
{
    IQueryable<Team> Teams { get; }
    IQueryable<Player> Players { get; }
    IQueryable<Coach> Coaches { get; }
}

和我的 DataContext 继承自 DbContext 和 ITeamDataSource

public class TeamDB : DbContext, ITeamDataSource
{
    public TeamDB()
        : base("DefaultConnection")
    {
        this.Configuration.LazyLoadingEnabled = false;
    }

    public DbSet<Team> Teams { get; set; }
    public DbSet<Player> Players { get; set; }
    public DbSet<Coach> Coaches { get; set; }

    IQueryable<Team> ITeamDataSource.Teams
    {
        get { return Teams; }
    }

    IQueryable<Player> ITeamDataSource.Players
    {
        get { return Players; }
    }

    IQueryable<Coach> ITeamDataSource.Coaches
    {
        get { return Coaches; }
    }
}

在我的种子方法中,我试图实现一些拥有球员名单的球队

        context.Teams.AddOrUpdate( t => t.Name,
          new Team { Name = "K1 White"},
          new Team { Name = "K1 Blue" },
          new Team { Name = "2nd Grade White" },
          new Team { Name = "2nd Grade Blue" },
          new Team { Name = "3rd Grade White" },
          new Team { Name = "3rd Grade Blue" },
          new Team { Name = "4th Grade White" },
          new Team { Name = "4th Grade Blue" }
        );

        context.SaveChanges();

        Team K1White = (Team)context.Teams.Where(t => t.Name == "K1 White").First();

        context.Players.AddOrUpdate(
            p => p.Name,
            new Player() { Name = "Alex S", Team = K1White, Position = "LG", Number = 26 },
            new Player() { Name = "Carson B", Team = K1White, Position = "RT", Number = 11 },
            new Player() { Name = "Colton B", Team = K1White, Position = "SUB", Number = 10 },
            new Player() { Name = "Christian H", Team = K1White, Position = "QB", Number = 17 },
            new Player() { Name = "Xander D", Team = K1White, Position = "RB", Number = 00 },
            new Player() { Name = "Zak B", Team = K1White, Position = "RB", Number = 15 },
            new Player() { Name = "Logan A", Team = K1White, Position = "RB", Number = 14 },
            new Player() { Name = "Solomon R", Team = K1White, Position = "TE", Number = 44 },
            new Player() { Name = "Wyatt G", Team = K1White, Position = "RT", Number = 99 },
            new Player() { Name = "Nick K", Team = K1White, Position = "C", Number = 98 },
            new Player() { Name = "Carson M", Team = K1White, Position = "RG", Number = 97},
            new Player() { Name = "Carson H", Team = K1White, Position = "TE", Number = 96 }
        );

        context.SaveChanges();

在我的控制器中,我只是简单地抓住所有团队并与他们一起返回视图

    public ActionResult Index()
    {
        var allTeams = _db.Teams;
        return View(allTeams);
    }

在我看来,我只是想创建一个球队名单,每个球队都有一个球员名单

@model IEnumerable<TeamManagement.Models.Team>

<ul>
    @foreach (var team in Model)
    {
        <li>@team.Name</li>
        <ul>
            @foreach (var player in team.Players)
            {
                <li>@player.Name | Position: @player.Position | Number: @player.Number</li>
            }
        </ul>
    }
</ul>

当我运行它时,它会在尝试遍历团队对象的玩家时失败,因为它为空。

我的问题基本上归结为:处理这个问题的正确方法是什么?

我没有在数据库方面做很多工作,也没有做过很多 EF CF 开发。我是否需要加载具有与我的控制器中的团队 ID 匹配的团队 ID 的所有玩家?我的模型设计错了吗?我没有正确地考虑它吗?

4

1 回答 1

1

我认为您可能会遇到类似于将实体分配给导航属性然后调用Add. 您可能会发现数据库中有很多同名的团队。

请参阅此说明:为什么实体框架将现有对象重新插入我的数据库

尝试添加一个TeamID到 Player 然后这样做:

var team1 = new Team { Name = "K1 White"};
context.Teams.AddOrUpdate( t => t.Name, team1);
context.Players.AddOrUpdate(
            p => p.Name,
            new Player() { Name = "Alex S", 
                           TeamID = team1.ID , Position = "LG", 
                           Number = 26 }
于 2013-10-30T15:36:17.323 回答