I just finished reading Adam Freeman's Pro ASP.NET MVC book as an introduction to ASP.NET MVC.
However, I ran into a situation where I have to join multiple tables but with Freeman's Method, I am unable to.
Please See my Code Below
EFEmployeeRepository.cs (In My Concrete Folder)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HRMS.Domain.Abstract;
using HRMS.Domain.Entities;
namespace HRMS.Domain.Concrete
{
public class EFEmployeeRepository : IEmployeesRepository
{
private EFDbContext context = new EFDbContext();
public Employee getEmployeeByID(int User_ID)
{
// I want to Join the Employee Table with the User Table to Get Employee Details
Employee employee = context.Employees.FirstOrDefault(p => p.User_ID == User_ID);
return employee;
}
}
}
IEmployeesRepository.cs (Absctract Folder)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HRMS.Domain.Entities;
namespace HRMS.Domain.Abstract
{
public interface IEmployeesRepository
{
//Get employee Details
Employee getEmployeeByID (int User_ID);
}
}
I will need to Join the Employee model with the user Model for a particular Employee and return the data to a controller
.Include(). Post the relevant parts of the classes.Employeeclass has aUserproperty, all you'd need to return the data for both entities would be to simply load theEmployeeclass. TheUserinstance would come along almost "transparently" through lazy or eager loading. You'd need to callInclude()only to force eager loading.