The INSERT statement conflicted with the FOREIGN KEY constraint - asp.net.mvc5

Viewed 145

Im getting that error when im trying to insert data to Tickets Table:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Tickets_dbo.Screenings_Date_HallId". The conflict occurred in database "aspnet-CinemaProject-20210105112627", table "dbo.Screenings". The statement has been terminated.

public class Ticket
{
    [Key]
    [Column(Order = 1)]
    public short SeatNumber { get; set; }


    [ForeignKey("Date, HallId")]
    public Screening Screening { get; set; }

    [Key]
    [Column(Order = 2)]
    public DateTime? Date { get; set; }


    [Key]
    [Column(Order = 3)]
    public byte HallId { get; set; }




    public DateTime? CreationTime { get; set; }

    public bool Paid { get; set; }
}

.

public class Screening
{
    [Key]
    [Column(Order = 1)]
    public DateTime? Date { get; set; }

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

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

    [Key]
    [Column(Order = 2)]
    [Required]
    [Display(Name = "Hall")]
    public byte HallId { get; set; }

    [Required]
    public short Price { get; set; }

}

.

public class Hall
{
    public byte Id { get; set; }
}

can any one help me?

EDIT:

Error

enter image description here

(Screening Table)^^

1 Answers

The error occurs because the date you are trying to insert in the "Ticket" table as a foreign key does NOT exist in the "screening" table as a primary key

Related