Introducing FOREIGN KEY constraint on table may cause cycles or multiple cascade paths. when creating database with Entity Framework

0

I am trying to create an app with database using Entity Framework while mostly following this tutorial (https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/intro?view=aspnetcore-5.0&tabs=visual-studio).

I have following models:

call.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations.Schema;

namespace ContactTracing15.Models
{
    public class Call
    {
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int CallID { get; set; }
        public int TracerID { get; set; }
        public int ContactID { get; set; }
        public DateTime Date { get; set; }

        public Tracer Tracer { get; set; }
        public Contact Contact { get; set; }
    }
}

tester.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ContactTracing15.Models
{
  public class Tester
  {
    public int TesterID { get; set; }

    public ICollection<Case> Cases { get; set; }
  }
}

contact.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations.Schema;

namespace ContactTracing15.Models
{
  public class Contact
  {
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int ContactID { get; set; }
    public string Forename { get; set; }
    public string Surname { get; set; }
    public string Email { get; set; }
    public string Postcode { get; set; }
    public string Phone { get; set; }
    public string? Phone2 { get; set; }
    public int TracerID { get; set; }
    public int CaseID { get; set; }
    public DateTime TestDate { get; set; }
    public DateTime AddedDate { get; set; }
    public DateTime? RemovedDate { get; set; }

    public Tracer Tracer { get; set; }
    public Case Case { get; set; }
    public ICollection<Call>? Calls { get; set; }

    }
}

(other models are not involved with the relationship)

When I try to build the app, database fails to be created due to following error which I dont really understand and couldn't find any clear info on the internet:

Microsoft.EntityFrameworkCore.Database.Command: Error: Failed executing DbCommand (6ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [Call] (
    [CallID] int NOT NULL,
    [TracerID] int NOT NULL,
    [ContactID] int NOT NULL,
    [Date] datetime2 NOT NULL,
    CONSTRAINT [PK_Call] PRIMARY KEY ([CallID]),
    CONSTRAINT [FK_Call_Contact_ContactID] FOREIGN KEY ([ContactID]) REFERENCES [Contact] ([ContactID]) ON DELETE CASCADE,
    CONSTRAINT [FK_Call_Tracer_TracerID] FOREIGN KEY ([TracerID]) REFERENCES [Tracer] ([TracerID]) ON DELETE CASCADE
);

Microsoft.Data.SqlClient.SqlException (0x80131904): Introducing FOREIGN KEY constraint 'FK_Call_Tracer_TracerID' on table 'Call' 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 or index. See previous errors.

What is it and how can I fix it?

asp.net
sql-server
entity-framework
asked on Stack Overflow Mar 23, 2021 by Tokyoline

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0