资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

自己改写的asp.netMVCEFRespoistory仓储模式

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

创新互联公司电话联系:13518219792,为您提供成都网站建设网页设计及定制高端网站建设服务,创新互联公司网页制作领域十年,包括成都凿毛机等多个领域拥有多年建站经验,选择创新互联公司,为网站保驾护航。

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 { 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 GetById(int id);

        IEnumerable SearchFor(Expression> predicate);

        IEnumerable 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 : IRepository where TEntity:class
    {
         

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

        }

        public Respoistory()
        {

        }

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

        public IEnumerable SearchFor(Expression> predicate)
        {
            return DbSet.Where(predicate);
        }

        public IEnumerable 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 Res_ArtCate = new Respoistory(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


新闻标题:自己改写的asp.netMVCEFRespoistory仓储模式
文章源于:http://cdkjz.cn/article/ppsgcj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220