在表“ReservedSeats”上引入 FOREIGN KEY 约束“FK_ReservedSeats_Seats_SeatId”可能会导致循环或多个级联路径

Posted

技术标签:

【中文标题】在表“ReservedSeats”上引入 FOREIGN KEY 约束“FK_ReservedSeats_Seats_SeatId”可能会导致循环或多个级联路径【英文标题】:Introducing FOREIGN KEY constraint 'FK_ReservedSeats_Seats_SeatId' on table 'ReservedSeats' may cause cycles or multiple cascade paths 【发布时间】:2020-12-20 14:34:41 【问题描述】:

我有一个电影院预订系统的数据库。

尝试使用 Seats 和 ReservedSeats 表更新数据库时出错:

。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。 无法创建约束。查看以前的错误。

座椅型号:

        [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; 

预留座位:

    [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; 

筛选:

[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; 

预订:

[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; 

电影:

        [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; 

礼堂:

 [Key]
public int Id  get; set; 

[Required]
public string Name  get; set; 

[Required]
public int SeatsNo  get; set; 

类型只有 id、名称和描述。

应用用户:

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

应用程序数据库上下文:

        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核心版本:3.1.7

【问题讨论】:

这能回答你的问题吗? Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why? 您的AuditoriumMovie 模型是什么?在我的asp.net core 3.1 项目中不使用这两个模型,它可以很好地工作。请您分享整个模型关系和您的dbcontext ?还有你的 ef 核心版本是什么? 我编辑了这篇文章。问题是我不知道如何为这个数据库设计另一个表结构。这个好像还行。我读到我需要在我的 ApplicationDbContext 中禁用级联删除,但我尝试的所有代码 sn-ps 都不起作用。或者也许我需要删除数据库,然后尝试创建表,同时仍然有代码?对不起。我是 .net core 的新手。 【参考方案1】:

我进行了创建 ReservedSeats 表的迁移,并为 SeatId FK 添加了 noAction onDelete 和 noAction onUpdate。

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

【讨论】:

以上是关于在表“ReservedSeats”上引入 FOREIGN KEY 约束“FK_ReservedSeats_Seats_SeatId”可能会导致循环或多个级联路径的主要内容,如果未能解决你的问题,请参考以下文章

在表“tblMaintenance”上引入 FOREIGN KEY 约束“FK2CustomerId”可能会导致循环或多个级联路径

ASP.Net MVC 3 EF“在表上引入 FOREIGN KEY 约束可能会导致循环或多个级联路径”

Thymeleaf 使用时的标签

MySQL基础

表中的简单 where 语句需要一个小时

Fore xcode 加载包