0

I have the Student, Course and a relationship between them as StudentCourse. The fields in these classes are as follows:

public class Student
    {
        public int StudentId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int CourseId { get; set; }
        [ForeignKey("CourseId")]
        public Course Course { get; set; }
    }


public class Course
    {
        public int CourseId { get; set; }
        public string CourseName { get; set; }
    }



public class StudentCourse
    {   
        public int ID { get; set; }
        public virtual Student Student { get; set; }
        public virtual Course Course {get;set;}
    }

When I delete the students in student table , then I want to remove the corresponding rows from the relationship StudentClass. How can I do it?

Bhushan Firake
  • 9,338
  • 5
  • 44
  • 79
  • @Slauma: Can you answer this please? – Bhushan Firake Jan 01 '13 at 08:39
  • Check this - http://stackoverflow.com/questions/5520418/entity-framework-code-first-delete-with-cascade – Sandeep Jan 01 '13 at 11:10
  • The model is strange. Don't you want a many-to-many relationship between `Student` and `Course`, i.e. a student can participate in *many* courses and a course can have *many* students? Normally you wouldn't need the `StudentCourse` entity because EF will manage the link table automatically. Can you explain why `Student` only has a single `Student.Course` reference and not a collection of courses? – Slauma Jan 01 '13 at 13:03
  • @Slauma I am newbie to Entity Framework , Dont know what is wrong.. – Bhushan Firake Jan 01 '13 at 14:49

1 Answers1

1

I believe that you actually want a many-to-many relationship between Student and Course: A student can participate in many courses and a course can have many students.

In this case you can simplify your model:

public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public ICollection<Course> Courses { get; set; }
}

public class Course
{
    public int CourseId { get; set; }
    public string CourseName { get; set; }
    public ICollection<Student> Students { get; set; }
}

The "join entity" StudentCourse is not needed. EF will create three tables from this model: A Students table, a Courses table and StudentCourses (or CourseStudents) table that will have a composite primary key (StudentId, CourseId) (or named similar). Both parts are foreign keys to their respective tables.

For the two FK relationships in the database cascading delete will be turned on by default. So, if a Student gets deleted the link records in the join table will be deleted automatically. The same when a Course gets deleted.

You can also define the detailed names for join table and join table columns explicity and you can also work with only a single collection, for example only the Courses collection in Student but without the Students collection in Course. You must use Fluent API for this:

modelBuilder.Entity<Student>()
    .HasMany(s => s.Courses)
    .WithMany() // no parameter if there is no collection in Course
    .Map(m =>
    {
        m.MapLeftKey("StudentId");
        m.MapRightKey("CourseId");
        m.ToTable("StudentCourses");
    });
Slauma
  • 175,098
  • 59
  • 401
  • 420
  • Can you update the answer giving details about composite foreign keys ? and deleting from only one table and not from the other foreign key table? – Bhushan Firake Jan 01 '13 at 18:07
  • @BhushanFirake: You don't need to care about composite keys in this example. EF manages this automatically for the join table. To delete only a `Student`: `context.Students.Remove(student);` It will delete the student and the related entries in the join table. It doesn't delete any courses. – Slauma Jan 01 '13 at 20:06