Self-rewriting ASP. NET MVC EF respoistory warehousing mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.