【发布时间】:2012-11-11 07:25:31
【问题描述】:
我有一个包含多个字段的表,这些字段是另一个表中主键的外键。例如:
Fixture Id (PK)
HomeTeamId (FK to Team.TeamId)
AwayTeamId (FK to Team.TeamId)
HomeTeamCoachId (FK to Coach.CoachId)
AwayTeamCoachId (FK to Coach.CoachId)
用 FixtureId 的外键将此数据分成 2 个表 HomeTeam 和 AwayTeam 会更好吗?这是目前由实体框架生成的:
FixtureId PK
HomeTeamId int
AwayTeamId int
HomeTeamCoachId int
AwayTeamCoachId int
AwayTeam_TeamId FK
HomeTeam_TeamId FK
AwayTeamCoach_CoachId FK
HomeTeamCoach_CoachId FK
这是通过这个类生成的:
public partial class Fixture
{
public int FixtureId { get; set; }
//foreign key
public int AwayTeamId { get; set; }
//navigation properties
public virtual Team AwayTeam { get; set; }
//foreign key
public int HomeTeamId { get; set; }
//navigation properties
public virtual Team HomeTeam { get; set; }
//foreign key
public int AwayCoachId { get; set; }
//navigation properties
public virtual Coach AwayCoach { get; set; }
//foreign key
public int HomeCoachId { get; set; }
//navigation properties
public virtual Coach HomeCoach { get; set; }
}
谁能告诉我这是否是正确的方法?
编辑:回复 Slauma
所以我的课程基本上是这样的?或者 OnModelCreating 中的配置是否意味着我的 Fixture 类中不需要一些与外键相关的代码?
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Entity Type Configuration
modelBuilder.Configurations.Add(new TeamConfiguration());
modelBuilder.Configurations.Add(new CoachConfiguration());
modelBuilder.Configurations.Add(new FixtureConfiguration());
modelBuilder.Entity<Fixture>()
.HasRequired(f => f.AwayTeam)
.WithMany()
.HasForeignKey(f => f.AwayTeamId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Fixture>()
.HasRequired(f => f.HomeTeam)
.WithMany()
.HasForeignKey(f => f.HomeTeamId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Fixture>()
.HasRequired(f => f.AwayCoach)
.WithMany()
.HasForeignKey(f => f.AwayCoachId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Fixture>()
.HasRequired(f => f.HomeCoach)
.WithMany()
.HasForeignKey(f => f.HomeCoachId)
.WillCascadeOnDelete(false);
}
public partial class Fixture
{
public int FixtureId { get; set; }
public string Season { get; set; }
public byte Week { get; set; }
//foreign key
public int AwayTeamId { get; set; }
//navigation properties
public virtual Team AwayTeam { get; set; }
//foreign key
public int HomeTeamId { get; set; }
//navigation properties
public virtual Team HomeTeam { get; set; }
//foreign key
public int AwayCoachId { get; set; }
//navigation properties
public virtual Coach AwayCoach { get; set; }
//foreign key
public int HomeCoachId { get; set; }
//navigation properties
public virtual Coach HomeCoach { get; set; }
public byte AwayTeamScore { get; set; }
public byte HomeTeamScore { get; set; }
}
【问题讨论】:
-
我认为对同一个表有多个 FK 引用没有任何问题(例如
Team) - 完全没问题。 -
技术上没问题。逻辑上可能。我假设球队和教练是相关的,所以主队不能只有任何个主教练。您可能想要建模一个 TeamCoach 表(FK 到 Team 和 Coach),并在
Fixture中使用TeamCoach引用,而不是分别使用Team和Coach。 -
团队和教练是相关的,但团队的教练可以更改,因此使用 TeamId 获取教练并不总是有效,因为此表将保存团队可能会经历多个不同教练的历史数据.
-
您可以拥有多个团队-教练组合(即使有历史记录)并选择其中两个作为固定装置。我可以想象你已经有一张这样的桌子,否则你将如何检查是否制作了正确的组合?
标签: entity-framework