Asp.net mvc 如何在ASP.Net MVC中使用CRUD,并在模式I中使用EntityFramework CodeFirst

Asp.net mvc 如何在ASP.Net MVC中使用CRUD,并在模式I中使用EntityFramework CodeFirst,asp.net-mvc,Asp.net Mvc,我在MVC中有一个电话簿项目,并使用IUnitOfWork。 但是我不知道这个项目是怎么做的。 项目的链接: 请为我介绍一下这个项目 我在这个项目中使用CRUD。我在我的项目中使用了通用回购和UoW,如下所示。你可以参考这一点来完成你的项目。我通常有4层解决方案体系结构: 核心 模型类 资料 一般回购和UoW DbContext 代码优先迁移 网 具有依赖注入实现的应用程序解决方案(例如Ninject) 试验 模型类 public class User { publ

我在MVC中有一个电话簿项目,并使用IUnitOfWork。 但是我不知道这个项目是怎么做的。 项目的链接:

请为我介绍一下这个项目


我在这个项目中使用CRUD。

我在我的项目中使用了通用回购和UoW,如下所示。你可以参考这一点来完成你的项目。我通常有4层解决方案体系结构:

  • 核心
    • 模型类
  • 资料
    • 一般回购和UoW
    • DbContext
    • 代码优先迁移
    • 具有依赖注入实现的应用程序解决方案(例如Ninject)
  • 试验

    模型类

    public class User
    {
            public int Id { get; set; }
            public string Name { get; set; }
    }
    
    public class Course
    {
            public int Id { get; set; }
            public string Name { get; set; }
    }
    
    public class UserController : Controller
        {
            private readonly IUserRepository _dbUserRepository;
    
            public UserController(IUserRepository dbUserRepository)
            {
                _dbUserRepository = dbUserRepository;
            }
    
            // GET: /User/
            public ActionResult Index()
            {
            var users = _dbUserRepository.GetAll();
    
                return View(users.ToList());
            }
    
           //Other CRUD operations
    
    }
    
  • MyDbContext.cs:

    public class MyDbContext : DbContext
        {
            public MyDbContext() : base("name=DefaultConnection”)
            {
            }
    
            public System.Data.Entity.DbSet<User> Users { get; set; }
            public System.Data.Entity.DbSet<Course> Courses { get; set; }
    
        }
    
    public class UnitOfWork : IUnitOfWork
        {
            //private variable for db context
            private MyDbContext _context;
            //initial db context variable when Unit of Work is constructed
            public UnitOfWork()
            {
                _context = new MyDbContext();
            }
            //property to get db context
            public MyDbContext Context
            {
                //if not null return current instance of db context else return new
                get { return _context ?? (_context = new MyDbContext()); }
            }
            //save function to save changes using UnitOfWork
            public void Save()
            {
                _context.SaveChanges();
            }
        }
    
    public class RepositoryBase<T> : IRepositoryBase<T> where T : class
        {
            protected readonly IUnitOfWork _unitOfWork;
            private readonly IDbSet<T> _dbSet;
    
            public RepositoryBase(IUnitOfWork unitOfWork)
            {
                _unitOfWork = unitOfWork;
                _dbSet = _unitOfWork.Context.Set<T>();
            }
    
        public virtual void Save()
            {
                _unitOfWork.Save();
            }
    
            public virtual void Add(T entity)
            {
                _dbSet.Add(entity);
                _unitOfWork.Save();
            }
        //Similarly you can have Update(), Delete(), GetAll() implementation here
    
        }
    
    public class UserRepository:RepositoryBase<User>,IUserRepository
        {
            public UserRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
            {
            }
        //Here you can also define functions specific to User
        }
    
    通用存储库:

    public class MyDbContext : DbContext
        {
            public MyDbContext() : base("name=DefaultConnection”)
            {
            }
    
            public System.Data.Entity.DbSet<User> Users { get; set; }
            public System.Data.Entity.DbSet<Course> Courses { get; set; }
    
        }
    
    public class UnitOfWork : IUnitOfWork
        {
            //private variable for db context
            private MyDbContext _context;
            //initial db context variable when Unit of Work is constructed
            public UnitOfWork()
            {
                _context = new MyDbContext();
            }
            //property to get db context
            public MyDbContext Context
            {
                //if not null return current instance of db context else return new
                get { return _context ?? (_context = new MyDbContext()); }
            }
            //save function to save changes using UnitOfWork
            public void Save()
            {
                _context.SaveChanges();
            }
        }
    
    public class RepositoryBase<T> : IRepositoryBase<T> where T : class
        {
            protected readonly IUnitOfWork _unitOfWork;
            private readonly IDbSet<T> _dbSet;
    
            public RepositoryBase(IUnitOfWork unitOfWork)
            {
                _unitOfWork = unitOfWork;
                _dbSet = _unitOfWork.Context.Set<T>();
            }
    
        public virtual void Save()
            {
                _unitOfWork.Save();
            }
    
            public virtual void Add(T entity)
            {
                _dbSet.Add(entity);
                _unitOfWork.Save();
            }
        //Similarly you can have Update(), Delete(), GetAll() implementation here
    
        }
    
    public class UserRepository:RepositoryBase<User>,IUserRepository
        {
            public UserRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
            {
            }
        //Here you can also define functions specific to User
        }
    

    您是否正在尝试理解UnitOfWork模式?请详细说明您的问题。是的,但我不知道如何在ServiceLayer和控制器以及Views中使用ViewModel。您的实现没有解耦。您仍然依赖于
    DbContext
    (您通过
    IUnitOfWork.Context
    公开的。在EF中,
    IUnitOfWork
    将为
    DbContext
    建模,您的存储库通常会为
    DbSet
    建模。
    DbContext
    的UnitOfWork实现公开存储库的接口并实例化具体实现。