Entity Framework include with left join is this possible?

后端 未结 3 476
面向向阳花
面向向阳花 2020-12-03 04:43

I have the following tables

  1. ClassRoom (ClassID,ClassName)
  2. StudentClass (StudentID,ClassID)
  3. Student (StudentID,StudentName,Etc..)
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 04:55

    I just had this problem, in my case it was the EntityTypeConfiguration that was wrong

    I had:

       HasRequired(s => s.ClassRoom)
                    .WithMany()
                    .HasForeignKey(student => student.ClassRoomId);
    

    Instead of:

       HasOptional(s => s.ClassRoom)
                    .WithMany()
                    .HasForeignKey(student => student.ClassRoomId);
    

    It seems HasRequired makes a INNER JOIN while HasOptional makes a LEFT JOIN.

提交回复
热议问题