The first is the model entity (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;}
}
}
Then the connection entity isDbContext (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;}
}
Then is the interface layer of the 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);
}
}
Then the respoistory layer (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 ();
}
}
}
Finally, how the controller layer calls the
Here I wrote a simple real column
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");
}
}
}
The above is a simple EF respoistry (warehouse).
Coffee: http://www.aicoffees.com/itshare/412081531.html
This article is from the "Coffee" blog, please be sure to keep this source http://aicoffees.blog.51cto.com/4410050/1587959
Self-rewriting ASP. NET MVC EF respoistory warehousing mode