1 数据访问层的编写
using SE17D.Demo3.MVC.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SE17D.Demo3.MVC.DAL
{
public class StudentDal
{
DbContext dbContext = new BIOContext();
//查询所有记录
public List<STUDENT> GetList()
{
return dbContext.Set<STUDENT>().ToList();
}
//查询单个记录
public STUDENT GetByID(int id)
{
return dbContext.Set<STUDENT>().Where(u => u.ID == id).FirstOrDefault();
}
//查询记录的条数
public int GetCount()
{
return dbContext.Set<STUDENT>().Count();
}
//添加记录
public int Add(STUDENT student)
{
dbContext.Set<STUDENT>().Add(student);
return dbContext.SaveChanges();
}
//修改记录
public int Update(STUDENT student)
{
dbContext.Set<STUDENT>().Attach(student);
dbContext.Entry(student).State = EntityState.Modified;
return dbContext.SaveChanges();
}
//删除记录
public int DeleteByID(int id)
{
STUDENT student = dbContext.Set<STUDENT>().Where(u => u.ID == id).FirstOrDefault();
dbContext.Entry(student).State = EntityState.Deleted;
return dbContext.SaveChanges();
}
}
}
2 业务逻辑层的编写
using SE17D.Demo3.MVC.DAL;
using SE17D.Demo3.MVC.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SE17D.Demo3.MVC.BLL
{
public class StudentBll
{
StudentDal studentDal = new StudentDal();
public List<STUDENT> GetList()
{
return studentDal.GetList();
}
public STUDENT GetByID(int id)
{
return studentDal.GetByID(id);
}
public int GetCount()
{
return studentDal.GetCount();
}
public bool Add(STUDENT person)
{
return studentDal.Add(person) > 0;
}
public bool Update(STUDENT person)
{
return studentDal.Update(person) > 0;
}
public bool DeleteByID(int id)
{
return studentDal.DeleteByID(id) > 0;
}
}
}
3 用户层的控制器书写
using SE17D.Demo3.MVC.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace SE17D.Demo3.MVC.UI.Controllers
{
public class StudentController : Controller
{
StudentBll studentBll = new StudentBll();
// GET: Student
public ActionResult Index()
{
return View(studentBll.GetList());
}
//public ActionResult Add()
//{
// return View();
//}
}
}
运行结果
网友评论