EF FOREIGN KEY 约束可能导致循环或多个级联路径

EF FOREIGN KEY constraint may cause cycles or multiple cascade paths

我正在开发一个示例应用程序,人们可以在其中对体育赛事下注并赚取积分。它具有以下 Entity Framework 代码优先模型:

public class Person
{
    [Key]
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }
}

public class Race
{
    [Key]
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }
}

public class RaceBet
{
    [Key]
    public int Id { get; set; }

    [Required]
    public int RaceId { get; set; }

    [Required]
    public int PersonId { get; set; }

    [Required]
    public int CompetitorId { get; set; }

    public virtual Race Race { get; set; }
    public virtual Person Person { get; set; }
    public virtual Person Competitor { get; set; }
}

一个人可以为一场比赛下注,他也可以对任何其他人(参赛者)下注。

模型会产生以下错误:

Introducing FOREIGN KEY constraint 'FK_dbo.RaceBets_dbo.People_PersonId' on table 'RaceBets' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint. See previous errors.

我尝试删除 OneToManyCascadeDeleteConvention,添加流畅的配置以防止 RaceBet 和 api 的所有其他变体的级联删除,但一切都失败了。

modelBuilder
.Entity<RaceBet>()
.HasRequired(x => x.Person)
.WithMany()
.HasForeignKey(x => x.PersonId)
.WillCascadeOnDelete(false);

我该如何解决这个问题?我的模型背后的概念是错误的吗?

谢谢!

感谢 Oleg 的评论:

I can't to reproduce exception with this code: protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Add(new System.Data.Entity.ModelConfiguration.Conventions.OneToManyCascadeDeleteConventi‌​on()); modelBuilder .Entity() .HasRequired(x => x.Person) .WithMany() .HasForeignKey(x => x.PersonId) .WillCascadeOnDelete(false); }

这修复了模型创建问题。