-1

I have a database for a Cinema Reservation System.

Error when trying to update-database with the Seats and ReservedSeats tables:

Introducing FOREIGN KEY constraint 'FK_ReservedSeats_Seats_SeatId' on table 'ReservedSeats' 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.

The model for Seat:

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

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

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

        [Required]
        public int AuditoriumId { get; set; }
        [ForeignKey("AuditoriumId")]
        public Auditorium Auditorium { get; set; }

ReservedSeat:

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

    public int SeatId { get; set; }
    [ForeignKey("SeatId")]
    public Seat Seat { get; set; }

    public int ScreeningId { get; set; }
    [ForeignKey("ScreeningId")]
    public Screening Screening { get; set; }

    public int ReservationId { get; set; }
    [ForeignKey("ResevationId")]
    public Reservation Reservation { get; set; }

    public bool isReserved { get; set; }

Screening:

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

[Required]
public int MovieId { get; set; }
[ForeignKey("MovieId")]
public Movie Movie { get; set; }

[Required]
public int AuditoriumId { get; set; }
[ForeignKey("AuditoriumId")]
public Auditorium Auditorium { get; set; }

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

[Required]

public double  TicketPrice { get; set; }

public string WeekDay { get; set; }

Reservation:

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

public int ScreeningId { get; set; }
[ForeignKey("ScreeningId")]
public Screening Screening { get; set; }

public IEnumerable<ReservedSeat> ReservedSeats { get; set; }

public string UserId { get; set; }
[ForeignKey("UserId")]
public ApplicationUser User { get; set; }
public double AmountToPay { get; set; }

public bool isPaid { get; set; }

public bool isCanceled { get; set; }

Movie:

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

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

        public string Director { get; set; }

        public string Description { get; set; }

        [Display(Name="Running Time")]
        public int RunningTimeMin { get; set; }


        public int GenreId { get; set; }
        [ForeignKey("GenreId")]
        public Genre Genre { get; set; }


        public string ImageUrl { get; set; }

Auditorium:

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

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

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

Genre only has id, name and description.

Application user:

   public string Name { get; set; }
        [NotMapped]
        public string Role { get; set; }

Application Db Context:

        public DbSet<Movie> Movies { get; set; }
        public DbSet<Genre> Genres { get; set; }
        public DbSet<Auditorium>  Auditoriums { get; set; }
        public DbSet<Screening> Screenings { get; set; }
        public DbSet<Seat> Seats { get; set; }
        public DbSet<ApplicationUser> ApplicationUsers { get; set; }
        public DbSet<ReservedSeat> ReservedSeats { get; set; }
        public DbSet<Reservation> Reservations { get; set; }

        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
            
        }

Ef core version: 3.1.7

  • 3
    Does this answer your question? [Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?](https://stackoverflow.com/questions/17127351/introducing-foreign-key-constraint-may-cause-cycles-or-multiple-cascade-paths) – David Browne - Microsoft Sep 01 '20 at 13:12
  • What is your `Auditorium` and `Movie` model?Without using the two models in my asp.net core 3.1 project,it could work well.Could you please share the whole model relationships and your dbcontext?Also what is your version of the ef core? – Rena Sep 02 '20 at 02:00
  • I edited the post. The thing is that I don't know how to design another table structure for this database. This one seemed okay. I read that I need to disable on cascade delete in my ApplicationDbContext, but all the snippets of code I tried didn't work. Or maybe I need to drop the database and then try to create the tables whilst still having the code there? I'm sorry. I'm new at .net core. – Friendly Minhyuk Sep 03 '20 at 07:29

1 Answers1

1

I went to the migration where I created the ReservedSeats Table and for the SeatId FK I added noAction onDelete and noAction onUpdate.

table.ForeignKey(
                name: "FK_ReservedSeats_Seats_SeatId",
                column: x => x.SeatId,
                principalTable: "Seats",
                principalColumn: "Id",
                onDelete: ReferentialAction.NoAction,
                onUpdate: ReferentialAction.NoAction);