我的问题标题很简单。我想我真正遇到的问题是了解我的对象如何相互关联以及这些关系如何存储在数据库中。
我有一个团队课程:
public class Team
{
public virtual int ID { get; set; }
public virtual string Name { get; set; }
public virtual IEnumerable<Player> Players { get; set; }
}
播放器类
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; }
}
和我继承自DbContext和ITeamDataSource
的DataContextpublic 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>
当我运行它时,它在尝试迭代团队对象的玩家时失败,因为它是null。
我的问题基本上归结为:处理此问题的正确方法是什么?
我没有在数据库中做过很多工作,也没有做过很多EF CF开发。我是否需要加载所有拥有与我控制器中团队ID相匹配的团队ID的玩家?我的设计在模特中是错的吗?我没有正确地思考它吗?
答案 0 :(得分:1)
我认为你可能会犯下类似于将实体分配给导航属性然后调用Add
时发生的事情。您可能会发现在数据库中有许多具有相同名称的团队。
请参阅此说明:Why does Entity Framework Reinsert Existing Objects into My Database
尝试向玩家添加TeamID
然后执行此操作:
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 }