Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Repository Using Entity Framework Code First 4.3

A while back i created repositories and services using linq to sql and I struggled to understand it. I finally understood it but Now I'm trying to do the same thing but using Code First EF. I'm confused on how this works with code first. If I have one repository that I can just pass in a class object and have select(), ect...How does this interact or how do I connect this to the/a DbContext? If someone can point me in the right direction or give me some advice it would be appreciated. Not much on this stuff on google since it's a relatively new pattern still.

How to use / would I use DbSet? These repositories are cool but confusing.

   public class IRepository<T> : IDisposable
        where T : class, new()
{
    IQueryable<T> Select();

    IQueryable<T> SelectWith(params Expression<Func<T, object>>[] includeProperties);

    T GetById(int id);

    T GetByIdWith(int id, params Expression<Func<T, object>>[] includeProperties);

    void InsertOnCommit(T model);

    void DeleteOnCommit(T model);

}


public class DataContext : DbContext
{
}
like image 440
TMan Avatar asked Mar 13 '12 02:03

TMan


People also ask

How do I use code first in Entity Framework?

Step 1 − First, create the console application from File → New → Project… Step 2 − Select Windows from the left pane and Console Application from the template pane. Step 3 − Enter EFCodeFirstDemo as the name and select OK. Step 4 − Right-click on your project in the solution explorer and select Manage NuGet Packages…

What is repository in Entity Framework?

The Repository Pattern allows us to create an abstraction layer between the data access layer and the business logic layer of an application. So, this Data Access Pattern offers a more loosely coupled approach to data access.

Does Entity Framework implement repository pattern?

No, the repository/unit-of-work pattern (shortened to Rep/UoW) isn't useful with EF Core. EF Core already implements a Rep/UoW pattern, so layering another Rep/UoW pattern on top of EF Core isn't helpful.


1 Answers

Here is a Tutorial of Repository and UnitOfWork in EF from ASP.Net. Hope this help. (UnitOfWork is to make sure multiple repositores share the same DataContext)

Generic Repository:

   public class GenericRepository<TEntity> where TEntity : class 
    { 
        internal SchoolDBContext context; 
        internal DbSet<TEntity> dbSet; 

        public GenericRepository(SchoolDBContext context) 
        { 
            this.context = context; 
            this.dbSet = context.Set<TEntity>(); 
        } 

        public virtual IEnumerable<TEntity> Get( 
          Expression<Func<TEntity, bool>> filter = null, 
          Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, 
          string includeProperties = "") 
        {
        ...
        }

        public virtual TEntity GetByID(object id) 
        { 
           return dbSet.Find(id); 
        }

        public virtual void Insert(TEntity entity) 
        { 
          dbSet.Add(entity); 
        } 

      ...
     }

UnitOfWork: Call the Save() method to update all your changes in repositories.

public class UnitOfWork : IDisposable 
{ 
    private SchoolDBContext context = new SchoolDBContext(); 
    private GenericRepository<Department> departmentRepository; 

    public GenericRepository<Department> DepartmentRepository 
    { 
        get 
        { 

            if (this.departmentRepository == null) 
            { 
                this.departmentRepository = new GenericRepository<Department>(context); 
            } 
            return departmentRepository; 
        } 
    }

    public void Save() 
    { 
        context.SaveChanges(); 
    }
    ....
 }

Controller:

 public class CourseController : Controller 
 { 
     private UnitOfWork unitOfWork = new UnitOfWork(); 

     // 
     // GET: /Course/ 

     public ViewResult Index() 
     { 
         var courses = unitOfWork.CourseRepository.Get(includeProperties: "Department"); 
         return View(courses.ToList()); 
    }

    ....
}
like image 148
Min Min Avatar answered Sep 30 '22 13:09

Min Min