K.Z
K.Z

Reputation: 5075

How to read iCollection data in repository pattern - ASP.NET- MVC Entity Framework

I am using repository pattern and unit of work in my ASP.NET-MVC application. I have generic repository for CRUD operation and service class between Unit of Work and controller class; meaning controller class call the unit of work to access all the operation which is great to encapsulate data access and business logic from web application.

now my question is how I can get icollection data within unit of work. taking example as below

enter image description here

Student Model

 public partial class Student
 {
    public Student()
    {
        this.StudentCourses = new HashSet<StudentCourse>();
    }

    public int StudentID { get; set; }
    public string Name { get; set; }

    public virtual ICollection<StudentCourse> StudentCourses { get; set; }
}

Course Model

public partial class Course
{
    public Course()
    {
        this.StudentCourses = new HashSet<StudentCourse>();
    }

    public int CourseID { get; set; }
    public string Title { get; set; }

    public virtual ICollection<StudentCourse> StudentCourses { get; set; }
}

StudentCourse Model

 public partial class StudentCourse
 {
    [Key]
    public int StudentCourseID { get; set; }

    [Key]
    [ForeignKey("Student")]
    public int StudentID { get; set; }

    [Key]
    [ForeignKey("Course")]
    public int CourseID { get; set; }

    public virtual Course Course { get; set; }
    public virtual Student Student { get; set; }
}

How I can achieve following LINQ query result using repository pattern and unit of work. I am struggling because generic repository classes only give students record but not courses unless I am missing something here. I am using data annotation not fluent API

 using (var db2 = new MyDbContext())
        {
            List<Student> _studentRead = new List<Student>();

            _studentRead = (from _student in db2.Students
                                .Include(r => r.StudentCourses.Select(sc => sc.Course))
                                select _student).ToList();

        }

Generic Repository Interface

 public interface IGenericRepository<TEntity> where TEntity :class
{

    global::System.Linq.IQueryable<TEntity> GetAll();
    TEntity GetEntityByID(int id);
    void InsertEntity(TEntity obj);
    void UpdateEntity(TEntity obj);
    void DeleteEntity(int id);   
}

Generic Repository

 public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
    protected DbSet<TEntity> _DbSet;
    private readonly DbContext _dbContext;

    public GenericRepository()
    { }

    public GenericRepository(DbContext dbContext)
    {
        this._dbContext = dbContext;
        _DbSet = _dbContext.Set<TEntity>();
    }

    public IQueryable<TEntity> GetAll()
    {
        return _DbSet;
    }

    public TEntity GetEntityByID(int id)
    {
        TEntity obj = _DbSet.Find(id);
        return obj;
    }

    public void InsertEntity(TEntity obj)
    {
        _DbSet.Add(obj);
    }

    public void UpdateEntity(TEntity obj)
    {
        _dbContext.Entry(obj).State = EntityState.Modified;
    }

    public void DeleteEntity(int id)
    {
        TEntity obj = _DbSet.Find(id);
        _DbSet.Remove(obj);
    }

}

Upvotes: 2

Views: 1561

Answers (1)

matt_lethargic
matt_lethargic

Reputation: 2796

To get courses you would just do the following:

var repository = new GenericRepository<Course>();
var courses = repository.GetAll();

To then use linq on those courses you can do:

var courses = from c in repository.GetAll()
              select c;

or

var courses = from c in repository.GetAll()
              where c.StudentCourses.StudentId == 1234
              select c;

or even:

var courses = repository.GetAll();
var studentCourses = from c in courses
              where c.StudentCourses.StudentId == 1234
              select c;

which is the same as:

var courses = repository.GetAll();
var studentCourses = courses.Where(x => x.StudentCourses.StudentId == 1234);

Hope this helps a bit, if not comment and be more specific as to what you;re trying to achieve.

Upvotes: 1

Related Questions