2

I have a DB stored locally with a number of different tables, all connected together like this:

enter image description here

I am trying to create a web API for these with entity framework on vs. My controller looks like this:

public class CourseController : ApiController
{
    QubDBEntities entities = new QubDBEntities();

    public IEnumerable<Course> Get()
    {
        using (QubDBEntities entities = new QubDBEntities())
        {
            return entities.Courses.ToList();
        }
    }

    public Course Get(Guid id)
    {
        using (QubDBEntities entities = new QubDBEntities())
        {
            return entities.Courses.FirstOrDefault(e => e.Id == id);
        }
    }

    public Course Get(string day, DateTime time, int duration,string moduleRef, string moduleName, string courseRef,
        string roomRef,string roomName, string roomFloor, int roomNumber, string buildingRef, string buildingName)
    {
        //how do I join tables inside here so that I can 
        //return the courses with this view?^
    }
}

I am not sure how to join these tables using entity framework? My SQL select statement here:

SELECT
    dbo.TimeTable.Day, dbo.TimeTable.StartTime, dbo.TimeTable.Duration, dbo.Module.ModuleRef
    , dbo.Module.ModuleName, dbo.Course.CourseRef, dbo.Room.RoomRef, dbo.Room.RoomName
    , dbo.Room.RoomFloor, dbo.Room.RoomNumber, dbo.Building.BuildingRef, dbo.Building.BuildingName

FROM dbo.Room INNER JOIN
         dbo.TimeTable INNER JOIN
         dbo.Module ON dbo.TimeTable.ModuleId = dbo.Module.Id ON dbo.Room.Id = dbo.TimeTable.RoomId INNER JOIN
         dbo.Building ON dbo.Room.BuildingId = dbo.Building.Id LEFT OUTER JOIN
         dbo.Course INNER JOIN
         dbo.CourseModule ON dbo.Course.Id = dbo.CourseModule.CourseId ON dbo.Module.Id = dbo.CourseModule.ModuleId

Any help / pointers would be appreciated!

1 Answer 1

1

There is three ways you can return results from joining tables in Entity Framework :
1- using Linq , please refer to link here
2- using include , please refer to link here
3- using raw sql like using generic function _context.Database.SqlQuery<type>(sqlString)

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

Comments

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.