温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

自己改写的asp.net MVC EF Respoistory 仓储模式

发布时间:2020-07-12 12:31:15 来源:网络 阅读:1409 作者:vs156663459 栏目:编程语言

首先是model 的实体(art_CategoryInfo.cs)

namespace BBS.Models
{
    using System;
    using System.Collections.Generic;
    
    public partial class art_CategoryInfo
    {
        public int Category_ID { get; set; }
        public string Category_title { get; set; }
        public string Category_description { get; set; }
        public string category_img { get; set; }
        public int Enabeld { get; set; }
    }
}


然后是连接实体的DbContext  (DB.cs)


using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using BBS.Models;

public partial class connetionEntities : DbContext
{
    public connetionEntities()
        : base("name=Entities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }


    public DbSet<art_CategoryInfo> art_CategoryInfo { get; set; }
 
}



然后是IRespoistory 的接口层(IRespoistory .cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
namespace BBS.Models
{
    public interface IRepository<TEntity>
    {

        TEntity GetById(int id);

        IEnumerable <TEntity> SearchFor(Expression<Func<TEntity, bool>> predicate);

        IEnumerable <TEntity> GetAll();

        void Edit(TEntity entity);

        void Insert(TEntity entity);

        void Delete(TEntity entity);
    }



}



然后是Respoistory层(Respoistory.cs




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BBS.Models;
using System.Linq.Expressions;
using System.Data.Entity;
using System.Data;
namespace BBS.Models
{
    public class Respoistory<TEntity> : IRepository<TEntity> where TEntity:class
    {
         

        protected DbSet<TEntity> DbSet;
        private readonly DbContext _dbContext;
        public Respoistory(DbContext dbcontext)
        {
            _dbContext = dbcontext;
            DbSet = _dbContext.Set<TEntity>();

        }

        public Respoistory()
        {

        }

        public TEntity GetById(int id)
        {
            return DbSet.Find(id);
        }
        

        public IEnumerable<TEntity> SearchFor(Expression<Func<TEntity, bool>> predicate)
        {
            return DbSet.Where(predicate);
        }

        public IEnumerable<TEntity> GetAll()
        {
            return DbSet;
        }

        public void Edit(TEntity entity)
        {
            _dbContext.Entry(entity).State = EntityState.Modified;
            _dbContext.SaveChanges();
        }

        public void Insert(TEntity entity)
        {
         
            DbSet.Add(entity);
            _dbContext.SaveChanges();
        }

        public void Delete(TEntity entity)
        {
            DbSet.Remove(entity);
            _dbContext.SaveChanges();
        }





    }
}





最后是Controller 层如何调用了


这里我写了简单的实列




HomeConcoller.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BBS.Models;
namespace BBS.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        Respoistory<art_CategoryInfo> Res_ArtCate = new Respoistory<art_CategoryInfo>(new connetionEntities());
        public ActionResult Index()
        {

            return View(Res_ArtCate.GetAll());


            
        }


        public ActionResult Create()
        {


            return View();
        }

        [HttpPost]
        public ActionResult Create(art_CategoryInfo art) 
        {
            if (ModelState.IsValid) 
            {
                Res_ArtCate.Insert(art);
                return RedirectToAction("Index");
            }
            return View(art);

        }




        public ActionResult Edit(int id) 
        {
            art_CategoryInfo arts = Res_ArtCate.GetById(id);
            if (arts == null) 
            {
                return HttpNotFound();
            }
            return View(arts);

        }

        [HttpPost]
        public ActionResult Edit(art_CategoryInfo art)
        {
            if (ModelState.IsValid) 
            {
                Res_ArtCate.Edit(art);
                return RedirectToAction("Index");
            }

            return View(art);
        }




        public ActionResult Delete(int id)
        {
            art_CategoryInfo art = Res_ArtCate.GetById(id);
            if (art == null)
            {
                return HttpNotFound();
            }
            return View(art);

        }



        [HttpPost,ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id) 
        {
            art_CategoryInfo art = Res_ArtCate.GetById(id);
            Res_ArtCate.Delete(art);
            return RedirectToAction("Index");


        }





    }
}


以上就是一个简单的EF   Respoistry(仓库)了

咖啡之念:http://www.aicoffees.com/itshare/412081531.html

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI