[英]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. 前一段时间,我使用linq to sql创建了存储库和服务,但我很难理解它。 I finally understood it but Now I'm trying to do the same thing but using Code First EF. 我终于理解了,但是现在我尝试使用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? 如果我有一个可以直接传入类对象并具有select()的存储库,则...该如何交互或如何将其连接到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. Google上的这些东西并不多,因为它仍然是一个相对较新的模式。
How to use / would I use DbSet? 如何使用/我将使用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
{
}
Here is a Tutorial of Repository and UnitOfWork in EF from ASP.Net . 这是ASP.Net中EF中的存储库和UnitOfWork的教程 。 Hope this help. 希望能有所帮助。 ( UnitOfWork is to make sure multiple repositores share the same DataContext ) ( UnitOfWork是为了确保多个存储库共享同一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. UnitOfWork :调用Save()方法更新存储库中的所有更改。
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());
}
....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.