I am trying to add a new student and after that, insert data into a junction table called 'AdditionalCourse' with the help of EF Core 3.1.
Whenever I add the junction rows (additional courses) by only using the foreign keys, it doesn't fill in the course property as expected after saving. It does fill in the Student property however.
Can someone figure out what I'm doing wrong?
I can solve this by just adding a 'get' call in between to just receive the freshly updated object, but I believe this is not necessary.
A minimal working prototype below:
Program.cs
static void Main(string[] args)
{
using (var context = new SchoolContext())
{
Course course = new Course("Spanish");
context.Add(course);
context.SaveChanges();
Course course2 = new Course("French");
context.Add(course2);
context.SaveChanges();
Student student = new Student("Bill");
context.Add(student);
context.SaveChanges();
AdditionalCourse addCourse1 = new AdditionalCourse() { CourseId = course.Id, StudentId = student.Id };
AdditionalCourse addCourse2 = new AdditionalCourse() { CourseId = course2.Id, StudentId = student.Id };
student.AdditionalCourses.Add(addCourse1);
student.AdditionalCourses.Add(addCourse2);
context.SaveChanges();
// Debug at this line
var x = 0;
}
}
AdditionalCourse junction table
public class AdditionalCourse
{
public int CourseId { get; set; }
public Course Course { get; set; }
public int StudentId { get; set; }
public Student Student { get; set; }
}
Student.cs
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public List<AdditionalCourse> AdditionalCourses { get; set; }
public Student()
{
}
public Student(string name)
{
Name = name;
AdditionalCourses = new List<AdditionalCourse>();
}
}
Course.cs
public class Course
{
public int Id { get; set; }
public string Name { get; set; }
public Course()
{
}
public Course(string name)
{
Name = name;
}
}
SchoolContext.cs
public class SchoolContext : DbContext
{
public SchoolContext()
{ }
public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
{ }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Data Source=SERVER_NAME;Initial Catalog=DATABASE_NAME;Integrated Security=True;");
}
public DbSet<Student> Students { get; set; }
public DbSet<Course> Courses { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>().ToTable("student");
modelBuilder.Entity<Course>().ToTable("course");
modelBuilder.Entity<AdditionalCourse>().ToTable("additional_course");
modelBuilder.Entity<AdditionalCourse>()
.HasKey(x => new { x.CourseId, x.StudentId });
modelBuilder.Entity<AdditionalCourse>()
.HasOne(x => x.Student)
.WithMany(x => x.AdditionalCourses)
.HasForeignKey(x => x.StudentId);
modelBuilder.Entity<AdditionalCourse>()
.HasOne(x => x.Course)
.WithMany()
.HasForeignKey(x => x.CourseId);
}
}
Course
object you expect already in the context you are using? Please provide a minimal reproducible example, which can be compiled and tested by others which shows that the navigation properties are not filled as expected.SaveChangesAsync()
withSaveChanges()
).