gpt4 book ai didi

entity-framework - 同一主键表的多个外键

转载 作者:行者123 更新时间:2023-12-03 07:27:08 25 4
gpt4 key购买 nike

我有一个包含多个字段的表,这些字段是另一个表中主键的外键。例如:

Fixture Id (PK)
HomeTeamId (FK to Team.TeamId)
AwayTeamId (FK to Team.TeamId)
HomeTeamCoachId (FK to Coach.CoachId)
AwayTeamCoachId (FK to Coach.CoachId)

将这些数据分成 2 个表 HomeTeam 和 AwayTeam 并使用 FixtureId 的外键会更好吗?这是当前由 Entity Framework 生成的:

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; }
}

谁能告诉我这是否是正确的方法?

编辑:回复斯劳玛

所以我的类(class)基本上是这样的?或者 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; }
}

最佳答案

显然 EF 不会将 int 属性(例如 AwayTeamId)检测为导航属性(例如 AwayTeam)的外键,因为主键属性Team 中的不是 Id 而是 TeamId。如果 FK 的命名类似于 AwayTeamTeamId,或者 Team 中的主键属性的名称为 Id,那么它可能会检测到 FK。

如果您不想根据 EF 约定更改这些属性名称,您可以使用数据注释定义 FK:

[ForeignKey("AwayTeam")]
public int AwayTeamId { get; set; }
public virtual Team AwayTeam { get; set; }

// the same for the other three FKs

或者 Fluent API:

modelBuilder.Entity<Fixture>()
.HasRequired(f => f.AwayTeam)
.WithMany()
.HasForeignKey(f => f.AwayTeamId)
.WillCascadeOnDelete(false);

// the same for the other three FKs

我已禁用级联删除,因为默认情况下将为所需关系启用它。但是,由于您与 Team 表(以及 Coach 表)有两个必需的关系,因此会导致来自 Fixture 的两个级联删除路径致团队教练。 SQL Server 中禁止多个级联删除路径,因此您必须对 FixtureTeam 之间(以及 Fixture教练)。

关于entity-framework - 同一主键表的多个外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13329369/

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