Noam Igor Veronin
Noam Igor Veronin

Reputation: 183

The INSERT statement conflicted with the FOREIGN KEY constraint FK_dbo.Courses_dbo.StudentCourses_CourseId

I am trying to insert some value into dbo.Course table but unfortunately I getting the following error, I tried to find a solution but I didn't understood how can I apply it on my Code.

My exception is:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Courses_dbo.StudentCourses_CourseId". The conflict occurred in database "MyStudentsWebSite11", table "dbo.StudentCourses", column 'Id'.

I try to run the following Query and once it reaching SaveChanges it crashes:

UneversityContext db = new UneversityContext();
var courses = new List<Course>
                    {
         new Course{CourseId=1, enter code CourseName="DotNet",StartDate="04/2018",CourseNumber="1111"},
                        new Course{CourseId=2, CourseName="WEB",StartDate="07/2018",CourseNumber="2222"},
                        new Course{CourseId=3,CourseName="Mobile",StartDate="08/2018",CourseNumber="3333"},
                         new Course{CourseId=4,CourseName="Mobile",StartDate="11/2018",CourseNumber="4444"},
                         new Course{CourseId=5,CourseName="WEB",StartDate="02/2018",CourseNumber="55555"}
                    };
                   courses.ForEach(x => db.Courses.Add(x));              
                      db.SaveChanges(); 

First Table

public class Course
    {
        [Key]
        public int CourseId { get; set; }

        public string CourseName { get; set; }

        public string StartDate { get; set; }

        public string CourseNumber { get; set; }
    }

Second Table

public class StudentCourse
    {
        [Key]
        public int Id { get; set; }
        public int StudentId { get; set; }
        public int CourseId { get; set; }

        [ForeignKey("CourseId")]
        public ICollection<Course> Courses { get; set; }

        [ForeignKey("StudentId")]
        public ICollection<Student> Students { get; set; }


    }

Upvotes: 0

Views: 507

Answers (2)

Turo
Turo

Reputation: 4924

I don't think you got Class StudentCourse right.

Either you make Course and Student a many to many Mapping (Course has a Collection of Students and Student has a Collection of Courses),

or you have StudentCourse with exactly one Student and one Course and make a mapping one to many from Student and Course to StudentCourse. But this makes only sense if you got additional attributes, like score or a sequence number etc.

Upvotes: 0

Vqf5mG96cSTT
Vqf5mG96cSTT

Reputation: 2911

If this code is all you are executing then your foreign key FK_dbo.Courses_dbo.StudentCourses_CourseId doesn't allow any Courses to be added for which there is no record in StudentCourses with the matching Id. This seems like a weird foreign key to me. It should be the other way around: you shouldn't be able to add a StudentCourses record without a matching Course record.

Upvotes: 2

Related Questions