简体   繁体   English

使用Entity Framework Code First 4.3创建存储库

[英]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.

相关问题 实体框架4.3代码首先是数据库命名 - Entity Framework 4.3 code first database naming 在Entity Framework 4.3中更新实体属性的正确方法-代码优先 - Proper Way to Update Properties of an Entity in Entity Framework 4.3 - Code First 存储库模式使用实体框架,代码优先和CRUD操作 - Repository Pattern using Entity Framework, code first and CRUD operations 首先使用实体​​框架代码为存储库实现通用方法 - Implementing generic methods for a repository using Entity Framework code first 首先使用Entity框架代码实现Generic Repository - Implementing Generic Repository using Entity framework code first 实体框架4.3代码首先使用相同的表多次多对多 - Entity Framework 4.3 code first multiple many to many using the same tables 如何首先使用迁移向 Entity Framework 4.3 代码中的列添加描述? - How to add description to columns in Entity Framework 4.3 code first using migrations? 在代码第一实体框架4.3中将自联接映射到集合 - Mapping a self-join to a collection in code first entity framework 4.3 实体框架4.3-代码优先-更新列表属性 - Entity Framework 4.3 - Code First - Update List Property 实体框架4.3 DBMigrations代码优先,数据库依赖性 - Entity framework 4.3 DBMigrations code first, database dependency
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM