我自己解决了问题。在我的回答下面,也许有人需要它。
比赛等级:
public class Match
{
[Key]
public int MatchID { get; set; }
public int HostID { get; set; }
[ForeignKey("HostID")]
public virtual Club Hosts{ get; set; }
public int GestID { get; set; }
[ForeignKey("GestID")]
public virtual Club Gests{ get; set; }
}
俱乐部等级:
public class Club
{
public int ClubID { get; set; }
public string ClubName{ get; set; }
public virtual ICollection<Match> Host{ get; set; }
public virtual ICollection<Match> Gest{ get; set; }
}
在 OnModelCreating 的 MyDbContext 中,我添加了以下代码:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Match>()
.HasRequired(x => x.Host)
.WithMany(x => x.Host)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Match>()
.HasRequired(x => x.Gest)
.WithMany(x => x.Gest)
.WillCascadeOnDelete(false);
}