0

I have this SQL Server query which works and gives the desired result in SQL Server:

SQL Server query:

SELECT
  s.RegNumber,
  s.AdmissionDate,
  c.CourseName
FROM student AS s
JOIN student_course AS sc
  ON s.id = sc.StudentId
JOIN course AS c
  ON c.id = sc.CourseId
WHERE c.Id = '67A21699-DFE6-4CB6-96B6-E009FD389596';

StudentCourses:

public class StudentCourses
{
    public Guid Id { get; set; }
    public Guid StudentId { get; set; }
    public Guid CourseId { get; set; }
}

However, when I tried to turn it to ASP.NET Core-6 Entity Framework as shown below:

var sc = Context.StudentCourses
                .Where(x => x.CourseId == '67A21699-DFE6-4CB6-96B6-E009FD389596')
                .Include(x => x.Student)
                .Include(x => x.Course);
return sc;

It gives me student details as null.

How do I get this sorted out?

Thanks.

11
  • What is the SQL column type of course.Id? Commented Jul 24, 2022 at 6:38
  • 4
    sc is an IQueryable. You need to call .ToListAsync to get the actual results. Commented Jul 24, 2022 at 6:39
  • @Dai - course.Id is Guid Commented Jul 24, 2022 at 6:40
  • x.CourseId == new Guid('67A21699-DFE6-4CB6-96B6-E009FD389596') Commented Jul 24, 2022 at 6:51
  • 2
    Are entity relationships properly modelled in your EF context? Typically, for many-to-many relationship the auxiliary table maintaining that relationship would not be represented as an entity. Commented Jul 24, 2022 at 7:20

1 Answer 1

1

In order to understand how to do that, you need to go back to basics

  1. You need to study how to build a model in EF. There are many tutorials on the web; and SO discourages opinions; so I will just put a link to official Microsoft documentation. You will end up with something like this:
public class Student
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public ICollection<Course> Courses { get; set; }
}

public class Course
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public ICollection<Student> Students { get; set; }
}

Note that there is no class StudentCourses!

  1. You need to study the implementation of many-to-many relationship in Entity Framework 6. Again, you should start at official Microsoft documentation; but it is a complex topic - so you probably need to spend more time on some tutorials. In DbContext you will have something like this:
modelBuilder.Entity<Students>()
            .HasMany(p => p.Courses)
            .WithMany(p => p.Students)

Once you have basic understanding of that, writing a LINQ statement is quite straightforward. Remember, you are NOT converting SQL to LINQ!

var sc = _context.Courses
    .Where(x => x.CourseId == '67A21699-DFE6-4CB6-96B6-E009FD389596')
    .Select(c => new { 
        c.CourseId,
        c.Name,
        c.Students
    });

Also note - that whenever you use Include(), it's "code smell"; it means you need to review your design, and maybe study some more tutorials.

Sign up to request clarification or add additional context in comments.

1 Comment

Why would the use of Include() be code smell?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.