自己改寫的asp.netMVCEFRespoistory倉儲模式-創(chuàng)新互聯(lián)

首先是model 的實體(art_CategoryInfo.cs)

成都創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)和重慶服務(wù)器托管的網(wǎng)絡(luò)公司,有著豐富的建站經(jīng)驗和案例。

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 層如何調(diào)用了

這里我寫了簡單的實列

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

創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。

分享文章:自己改寫的asp.netMVCEFRespoistory倉儲模式-創(chuàng)新互聯(lián)
文章鏈接:http://muchs.cn/article44/dpsjhe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、App開發(fā)、服務(wù)器托管、網(wǎng)站設(shè)計、用戶體驗、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管