1

I am trying to have a model with two references to another model.

I have researched this error, but have seen suggestions relating to previous .Net Core versions.

Below is the model

public class Match
{
    public int ID { get; set; }
    public DateTime MatchDateTime
    { get; set; }
    public int LocationID { get; set; }
    public int GroupID { get; set; }
    public int HomeTeamScore { get; set; }
    public int AwayTeamScore { get; set; }
    [Required]
    public int HomeTeamID { get; set; }
    public int AwayTeamID { get; set; }
    public Location Location { get; set; }
    public Group Group { get; set; }

    [Required]
    [ForeignKey("HomeTeamID")]
    public Team HomeTeam { get; set; }
    [ForeignKey("AwayTeamID")]
    public Team AwayTeam { get; set; }
}

Upon running the migration, i get this error:

Introducing FOREIGN KEY constraint 'FK_Matches_Teams_AwayTeamID' on table 'Matches' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

8
  • Can you provide us OnModelCreating method contents (since you're stated using Code First migrations)? I think you have problem in modelBuilder.Entity<Match> relationship which may declared more than once, causing multiple cycles or cascade paths. Commented Apr 18, 2018 at 4:24
  • Set your fk onelete to restrict instead of cascade Commented Apr 18, 2018 at 7:32
  • The error isn't related to any .Net version, it's a SQL Server error that pretty clearly tells you shat's wrong. The two FKs to Team can't both have cascaded delete. Commented Apr 18, 2018 at 10:59
  • @TetsuyaYamamoto: I have no code in my OnModelCreating() function Commented Apr 18, 2018 at 14:57
  • @Mardoxx: I am not sure what DataAnnotation I should use to accomplish that. Commented Apr 18, 2018 at 14:58

2 Answers 2

2

When you run Add-Migration, it will generate migration file similar to the below code.

migrationBuilder.CreateTable(
name: "Match",
columns: table => new
{
    ID = table.Column<int>(nullable: false)
        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
    AwayTeamID = table.Column<int>(nullable: false),
    AwayTeamScore = table.Column<int>(nullable: false),
    GroupID = table.Column<int>(nullable: false),
    HomeTeamID = table.Column<int>(nullable: false),
    HomeTeamScore = table.Column<int>(nullable: false),
    LocationID = table.Column<int>(nullable: false),
    MatchDateTime = table.Column<DateTime>(nullable: false)
},
constraints: table =>
{
    table.PrimaryKey("PK_Match", x => x.ID);
    table.ForeignKey(
        name: "FK_Match_Team_AwayTeamID",
        column: x => x.AwayTeamID,
        principalTable: "Team",
        principalColumn: "Id",
        onDelete: ReferentialAction.Cascade);
    table.ForeignKey(
        name: "FK_Match_Team_HomeTeamID",
        column: x => x.HomeTeamID,
        principalTable: "Team",
        principalColumn: "Id",
        onDelete: ReferentialAction.Cascade);
});

Set ReferentialAction.NoAction for FK_Match_Team_HomeTeamID.

Sign up to request clarification or add additional context in comments.

Comments

1

So..The solution was simple really.

I made the Foreign Key IDs nullable. I am not sure if making both nullable was 'required'

See the code below:

public class Match
{
public int ID { get; set; }
public DateTime MatchDateTime
{ get; set; }
public int LocationID { get; set; }
public int GroupID { get; set; }
public int HomeTeamScore { get; set; }
public int AwayTeamScore { get; set; }
public int? HomeTeamID { get; set; }
public int? AwayTeamID { get; set; }
public Location Location { get; set; }
public Group Group { get; set; }

[Required]
[ForeignKey("HomeTeamID")]
public Team HomeTeam { get; set; }
[ForeignKey("AwayTeamID")]
public Team AwayTeam { get; set; }

}

1 Comment

What if you Require nullable, then?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.