ASP.NET MVC5+EF6+LayUI实战教程,通用后台管理系统框架(7)- EF增删改查
前言
上一节创建了实体数据库,这次我们来看看怎么操作这个实体
代码实现
新建一个UserInfoController的控制器:不需要写什么代码,系统自动生成Index方法:
创建IDAL,DAL,IBLL,BLL的代码:
using BYCMS.Model; using System.Linq; namespace BYCMS.IDAL { public interface IUserInfoRepository { /// <summary> /// 获取列表 /// </summary> /// <param name="db">数据库上下文</param> /// <returns>数据列表</returns> IQueryable<UserInfo> GetList(DBContainer db); /// <summary> /// 创建一个实体 /// </summary> /// <param name="entity">实体</param> int Create(UserInfo entity); /// <summary> /// 删除一个实体 /// </summary> /// <param name="entity">主键ID</param> int Delete(int id); /// <summary> /// 修改一个实体 /// </summary> /// <param name="entity">实体</param> int Edit(UserInfo entity); /// <summary> /// 获得一个实体 /// </summary> /// <param name="id">id</param> /// <returns>实体</returns> UserInfo GetById(int id); /// <summary> /// 判断一个实体是否存在 /// </summary> bool IsExist(int id); } }
using BYCMS.IDAL; using BYCMS.Model; using System.Data.Entity; using System.Linq; namespace BYCMS.DAL { public class UserInfoRepository : IUserInfoRepository { /// <summary> /// 创建一个实体 /// </summary> /// <param name="entity">实体</param> /// <returns></returns> public int Create(UserInfo entity) { using (DBContainer db = new DBContainer()) { db.Set<UserInfo>().Add(entity); return db.SaveChanges(); } } /// <summary> /// 删除一个实体 /// </summary> /// <param name="id">主键ID</param> /// <returns></returns> public int Delete(int id) { using (DBContainer db = new DBContainer()) { UserInfo entity = db.UserInfoSet.Find(id); db.Set<UserInfo>().Remove(entity); return db.SaveChanges(); } } /// <summary> /// 修改一个实体 /// </summary> /// <param name="entity">实体</param> /// <returns></returns> public int Edit(UserInfo entity) { using (DBContainer db = new DBContainer()) { db.Set<UserInfo>().Attach(entity); db.Entry(entity).State = EntityState.Modified; return db.SaveChanges(); } } /// <summary> /// 获得一个实体 /// </summary> /// <param name="id">主键ID</param> /// <returns></returns> public UserInfo GetById(int id) { using (DBContainer db = new DBContainer()) { return db.UserInfoSet.Find(id); } } /// <summary> /// 获取列表 /// </summary> /// <param name="db"></param> /// <returns></returns> public IQueryable<UserInfo> GetList(DBContainer db) { IQueryable<UserInfo> list = db.UserInfoSet.AsQueryable(); return list; } /// <summary> /// 判断一个实体是否存在 /// </summary> /// <param name="id">主键ID</param> /// <returns></returns> public bool IsExist(int id) { using (DBContainer db = new DBContainer()) { UserInfo entity = GetById(id); if (entity != null) return true; return false; } } } }
using BYCMS.Model; using System.Collections.Generic; namespace BYCMS.IBLL { public interface IUserInfoBLL { /// <summary> /// 获取列表 /// </summary> /// <param name="pager">JQgrid分页</param> /// <param name="queryStr">搜索条件</param> /// <returns>列表</returns> List<UserInfo> GetList(); /// <summary> /// 创建一个实体 /// </summary> /// <param name="errors">持久的错误信息</param> /// <param name="model">模型</param> /// <returns>是否成功</returns> bool Create(UserInfo model); /// <summary> /// 删除一个实体 /// </summary> /// <param name="errors">持久的错误信息</param> /// <param name="id">id</param> /// <returns>是否成功</returns> bool Delete(int id); /// <summary> /// 修改一个实体 /// </summary> /// <param name="errors">持久的错误信息</param> /// <param name="model">模型</param> /// <returns>是否成功</returns> bool Edit(UserInfo model); /// <summary> /// 根据ID获得一个Model实体 /// </summary> /// <param name="id">id</param> /// <returns>Model实体</returns> UserInfo GetById(int id); /// <summary> /// 判断是否存在实体 /// </summary> /// <param name="id">主键ID</param> /// <returns>是否存在</returns> bool IsExist(int id); } }
using BYCMS.Common; using BYCMS.DAL; using BYCMS.IBLL; using BYCMS.IDAL; using BYCMS.Model; using System.Collections.Generic; using System.Linq; namespace BYCMS.BLL { public class UserInfoBLL : IUserInfoBLL { DBContainer db = new DBContainer(); IUserInfoRepository rep = new UserInfoRepository(); /// <summary> /// 创建一个实体 /// </summary> /// <param name="errors"></param> /// <param name="model"></param> /// <returns></returns> public bool Create(UserInfo model) { if (rep.Create(model) == 1) { return true; } return false; } /// <summary> /// 根据id删除实体 /// </summary> /// <param name="errors"></param> /// <param name="id"></param> /// <returns></returns> public bool Delete(int id) { if(rep.Delete(id) > 0) { return true; } return false; } /// <summary> /// 编辑实体 /// </summary> /// <param name="errors"></param> /// <param name="model"></param> /// <returns></returns> public bool Edit(UserInfo model) { if (rep.Edit(model) > 0) { return true; } return false; } /// <summary> /// 根据id查找实体 /// </summary> /// <param name="id"></param> /// <returns></returns> public UserInfo GetById(int id) { return rep.GetById(id); } /// <summary> /// 实体列表 /// </summary> /// <param name="pager"></param> /// <param name="queryStr"></param> /// <returns></returns> public List<UserInfo> GetList() { IQueryable<UserInfo> queryData = rep.GetList(db); return queryData.ToList(); } /// <summary> /// 判断是否存在实体 /// </summary> /// <param name="id"></param> /// <returns></returns> public bool IsExist(int id) { return rep.IsExist(id); } } }
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>用户中心</title> <meta name="renderer" content="webkit"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="format-detection" content="telephone=no"> <link rel="stylesheet" href="~/Content/layui/css/layui.css" media="all" /> <link rel="stylesheet" href="~/Content/css/public.css" media="all" /> </head> <body class="childrenBody"> <blockquote class="layui-elem-quote quoteBox"> <form class="layui-form"> <div class="layui-inline"> <div class="layui-input-inline"> <input type="text" class="layui-input searchVal" placeholder="请输入搜索的内容" /> </div> <a class="layui-btn search_btn" data-type="reload">搜索</a> </div> <div class="layui-inline"> <a class="layui-btn layui-btn-normal addNews_btn">添加用户</a> </div> <div class="layui-inline"> <a class="layui-btn layui-btn-danger layui-btn-normal delAll_btn">批量删除</a> </div> </form> </blockquote> <table id="userList" lay-filter="userList"></table> <!--操作--> <script type="text/html" id="userListBar"> <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a> <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="usable">已启用</a> <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a> </script> <script type="text/javascript" src="~/Content/layui/layui.js"></script> <script type="text/javascript" src="~/Content/js/user/userList.js"></script> </body> </html>
我们往数据库插入几条记录这时你应该看到效果了
每天进步一点,生活更你好一点