MVC ---- EF三层代码
1、DAL层
using Night.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection; namespace NightCat.DAL
{
public class SysLogService
{ //public SysLogService()
//{
// //删除使用
// DelByCondition(p => p.F_Id == "00003"); // //修改使用
// Sys_Log sl = new Sys_Log()
// {
// F_Id = "00003",
// F_Account = "adf",
// F_CreatorTime = DateTime.Now
// };
// this.Modify(sl, "F_Account", "F_CreatorTime");//或
// this.Modify(sl, new string[] { "F_Account", "F_CreatorTime" }); // //排序
// List<Sys_Log> ssl = this.GetListByCondition(p => p.F_Id == "00003" && p.F_Account == "1000");
// List<Sys_Log> ssl2 = this.GetListByCondition(p => p.F_Id == "00003" && p.F_Account == "1000", p2 => p2.F_Id); //} /// <summary>
/// EF数据上下文
/// </summary>
NightCatEntities nc = new NightCatEntities(); #region 新增实体
/// <summary>
/// 新增实体
/// </summary>
/// <param name="log"></param>
/// <returns></returns>
public int Add(Sys_Log log)
{
try
{
nc.Sys_Log.Add(log);
//保存成功之后,会将自增的ID和新增的行数设置给log
return nc.SaveChanges();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion #region 根据ID删除一条数据
/// <summary>
/// 根据ID删除一条数据
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public int DelById(string Id)
{
try
{
//创建对象
Sys_Log sl = new Sys_Log()
{
F_Id = Id
};
//把要删除的对象添加到集合中
nc.Sys_Log.Attach(sl);
nc.Sys_Log.Remove(sl);
return nc.SaveChanges();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion #region 根据条件删除数据
/// <summary>
/// 根据条件删除数据
/// </summary>
/// <param name="delWhere">返回值为bool类型的委托对象(方法)</param>
/// <returns></returns>
public int DelByCondition(Expression<Func<Sys_Log, bool>> delWhere)
{
try
{
//查询
List<Sys_Log> pDels = nc.Sys_Log.Where(delWhere).ToList();
pDels.ForEach(p => nc.Sys_Log.Remove(p));
return nc.SaveChanges();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion #region 修改+Modify(Sys_Log p, params string[] proNames)
/// <summary>
/// 修改数据
/// </summary>
/// <param name="p">要修改的对象</param>
/// <param name="proName">要修改的属性名称集合</param>
/// params 可变参数
/// <returns></returns>
public int Modify(Sys_Log p, params string[] proNames)
{
try
{
DbEntityEntry entry = nc.Entry<Sys_Log>(p);
entry.State = System.Data.Entity.EntityState.Unchanged; foreach (string proName in proNames)
{
entry.Property(proName).IsModified = true;
}
return nc.SaveChanges();
}
catch (Exception)
{ throw;
}
}
#endregion #region 根据条件检索集合中的数据 List<Sys_Log> GetListByCondition(Expression<Func<Sys_Log, bool>> whereLambda)
/// <summary>
/// 根据条件检索集合中的数据
/// </summary>
/// <param name="whereLambda"></param>
/// <returns></returns>
public List<Sys_Log> GetListByCondition(Expression<Func<Sys_Log, bool>> whereLambda)
{
try
{
return nc.Sys_Log.Where(whereLambda).ToList();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion #region + 根据条件检索集合中的数据并进行排序+GetListByCondition<Tkey>(Expression<Func<Sys_Log, bool>> whereLambda,Expression<Func<Sys_Log,Tkey>> orderLamba)
/// <summary>
/// 根据条件检索集合中的数据并进行排序+GetListByCondition<Tkey>(Expression<Func<Sys_Log, bool>> whereLambda,Expression<Func<Sys_Log,Tkey>> orderLamba)
/// </summary>
/// <param name="whereLambda"></param>
/// <returns></returns>
/// 这么的Tkey不是必要的使用的时候可以不写
public List<Sys_Log> GetListByCondition<Tkey>(Expression<Func<Sys_Log, bool>> whereLambda,Expression<Func<Sys_Log,Tkey>> orderLamba)
{
try
{
return nc.Sys_Log.Where(whereLambda).OrderBy(orderLamba).ToList();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion #region 根据条件和排序查询一分页的形式展示
/// <summary>
/// 根据条件和排序查询一分页的形式展示
/// </summary>
/// <typeparam name="Tkey">排序字段类型</typeparam>
/// <param name="pageIndex">查询的页面</param>
/// <param name="pageSize">每页显示的条数</param>
/// <param name="orderLambda">排序的字段</param>
/// <param name="whereLambda">查询的条件</param>
/// <returns></returns>
public List<Sys_Log> GetPageList<Tkey>(int pageIndex, int pageSize, Expression<Func<Sys_Log, Tkey>> orderLambda, Expression<Func<Sys_Log, bool>> whereLambda)
{
try
{
return nc.Sys_Log.Where(whereLambda).OrderBy(orderLambda).Skip((pageIndex - ) * pageSize).Take(pageSize).ToList();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion #region 批量修改+Modify(Sys_Log p, params string[] proNames)
/// <summary>
/// 批量修改数据
/// </summary>
/// <param name="model">要修改的列及修改后列的值的集合</param>
/// params 可变参数
/// <returns></returns>
public int Modifys(Sys_Log model,Expression<Func<Sys_Log,bool>> whereLambda,params string[] ModifyProNames)
{
try
{
//1、查询出要修改的数据
List<Sys_Log> listModif = nc.Sys_Log.Where(whereLambda).ToList(); //2、获取实体类对象
Type t = typeof(Sys_Log);
//3、获取实体类所有的共有属性
List<Sys_Log> proInfos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList();
//4、创建一个实体属性字典集合
Dictionary<string, PropertyInfo> dictPros = new Dictionary<string, PropertyInfo>();
//5、将实体属性中要修改的属性,添加到字典集合中,键:属性名,值:属性对象
proInfos.ForEach(p =>
{
if (ModifyProNames.Contains(p.F_NickName))
{
dictPros.Add(p.F_NickName, p);
}
});
//循环要修改的属性名
foreach (string proName in ModifyProNames)
{
//判断要修改的属性名是否在实体类的属性集合中
if (dictPros.ContainsKey(proName))
{
//如何存在,则取出要修改的属性对象
PropertyInfo proInfo = dictPros[proName];
//取出要修改的值
object newValue = proInfo.GetValue(model, null);
//批量设置要修改的对象的属性
foreach (Sys_Log item in listModif)
{
//为要修改的对象的要修改的属性,设置新的值
proInfo.SetValue(item, newValue,null);
} }
} }
catch (Exception)
{ throw;
}
}
#endregion
}
}
2、BLL层
using Night.Models;
using NightCat.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using NightCat.DAL; namespace NightCat.BLL
{
public class SysLogManager
{
SysLogService ss = new SysLogService(); #region 新增实体
/// <summary>
/// 新增实体
/// </summary>
/// <param name="log"></param>
/// <returns></returns>
public int Add(Sys_Log log)
{
try
{
return ss.Add(log);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion #region 根据ID删除一条数据
/// <summary>
/// 根据ID删除一条数据
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public int DelById(string Id)
{
try
{
return ss.DelById(Id);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion #region 根据条件删除数据
/// <summary>
/// 根据条件删除数据
/// </summary>
/// <param name="delWhere">返回值为bool类型的委托对象(方法)</param>
/// <returns></returns>
public int DelByCondition(Expression<Func<Sys_Log, bool>> delWhere)
{
try
{
//查询
return ss.DelByCondition(delWhere);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion #region 修改+Modify(Sys_Log p, params string[] proNames)
/// <summary>
/// 修改数据
/// </summary>
/// <param name="p">要修改的对象</param>
/// <param name="proName">要修改的属性名称集合</param>
/// params 可变参数
/// <returns></returns>
public int Modify(Sys_Log p, params string[] proNames)
{
try
{
return ss.Modify(p, proNames);
}
catch (Exception)
{ throw;
}
}
#endregion #region 根据条件检索集合中的数据 List<Sys_Log> GetListByCondition(Expression<Func<Sys_Log, bool>> whereLambda)
/// <summary>
/// 根据条件检索集合中的数据
/// </summary>
/// <param name="whereLambda"></param>
/// <returns></returns>
public List<Sys_Log> GetListByCondition(Expression<Func<Sys_Log, bool>> whereLambda)
{
try
{
return ss.GetListByCondition(whereLambda);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion #region + 根据条件检索集合中的数据并进行排序+GetListByCondition<Tkey>(Expression<Func<Sys_Log, bool>> whereLambda,Expression<Func<Sys_Log,Tkey>> orderLamba)
/// <summary>
/// 根据条件检索集合中的数据并进行排序+GetListByCondition<Tkey>(Expression<Func<Sys_Log, bool>> whereLambda,Expression<Func<Sys_Log,Tkey>> orderLamba)
/// </summary>
/// <param name="whereLambda"></param>
/// <returns></returns>
/// 这么的Tkey不是必要的使用的时候可以不写
public List<Sys_Log> GetListByCondition<Tkey>(Expression<Func<Sys_Log, bool>> whereLambda, Expression<Func<Sys_Log, Tkey>> orderLamba)
{
try
{
return ss.GetListByCondition<Tkey>(whereLambda,orderLamba);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion #region 根据条件和排序查询一分页的形式展示
/// <summary>
/// 根据条件和排序查询一分页的形式展示
/// </summary>
/// <typeparam name="Tkey">排序字段类型</typeparam>
/// <param name="pageIndex">查询的页面</param>
/// <param name="pageSize">每页显示的条数</param>
/// <param name="orderLambda">排序的字段</param>
/// <param name="whereLambda">查询的条件</param>
/// <returns></returns>
public List<Sys_Log> GetPageList<Tkey>(int pageIndex, int pageSize, Expression<Func<Sys_Log, Tkey>> orderLambda, Expression<Func<Sys_Log, bool>> whereLambda)
{
try
{
return ss.GetPageList(pageIndex, pageSize, orderLambda, whereLambda);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}
3、UI层
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="NightCatWeb.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvParas" runat="server"> </asp:GridView>
</div>
</form>
</body>
</html>
---------------------------------------------------------------------
using Night.Models;
using NightCat.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace NightCatWeb
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
SysLogManager sm = new SysLogManager();
List<Sys_Log> sl = sm.GetPageList(, , p => p.F_Id, p => p.F_IPAddress != "").ToList();
this.gvParas.DataSource = sl;
this.gvParas.DataBind();
}
}
}
}
MVC ---- EF三层代码的更多相关文章
- 转载——Asp.Net MVC+EF+三层架构的完整搭建过程
转载http://www.cnblogs.com/zzqvq/p/5816091.html Asp.Net MVC+EF+三层架构的完整搭建过程 架构图: 使用的数据库: 一张公司的员工信息表,测试数 ...
- MVC+EF三层+抽象工厂
MVC+EF三层+抽象工厂项目搭建 注意:项目经过两次搭建,所以截图中顶级命名空间有ZHH和ZHH2区别,但是架构的内容是一样的,可以将ZHH和ZHH2视为同一命名空间 一:权限管理 二:搜索 | ...
- Asp.Net MVC+EF+三层架构的完整搭建过程
架构图: 使用的数据库: 一张公司的员工信息表,测试数据 解决方案项目设计: 1.新建一个空白解决方案名称为Company 2.在该解决方案下,新建解决方案文件夹(UI,BLL,DAL,Model) ...
- Asp.Net MVC+EF+三层架构
架构图: 使用的数据库: 一张公司的员工信息表,测试数据 解决方案项目设计: 1.新建一个空白解决方案名称为Company 2.在该解决方案下,新建解决方案文件夹(UI,BLL,DAL,Model) ...
- 瞧一瞧,看一看呐,用MVC+EF快速弄出一个CRUD,一行代码都不用写,真的一行代码都不用写!!!!
瞧一瞧,看一看呐用MVC+EF快速弄出一个CRUD,一行代码都不用写,真的一行代码都不用写!!!! 现在要写的呢就是,用MVC和EF弄出一个CRUD四个页面和一个列表页面的一个快速DEMO,当然是在不 ...
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(3)-面向接口的编程
原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(3)-面向接口的编程 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) (1)框架搭建 (2):数据 ...
- 【MVC 1】MVC+EF实体框架—原理解析
导读:在之前,我们学过了三层框架,即:UI.BLL.DAL.我们将页面显示.逻辑处理和数据访问进行分层,避免了一层.两层的混乱.而后,我们又在经典三层的基础上,应用设计模式:外观.抽象工厂+反射,使得 ...
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(24)-权限组的设计和实现(附源码)(终结)
ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) (1):框架搭建 (2):数据库访问层的设计Demo (3):面向接口编程 (4 ):业务逻辑层的封装 ...
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(23)-设置角色遗留问题和为权限设置角色以及EasyUI Tabs的使用
ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) (1):框架搭建 (2):数据库访问层的设计Demo (3):面向接口编程 (4 ):业务逻辑层的封装 ...
随机推荐
- Vue 命令
vue是数据渲染使用:axios,官网:https://www.kancloud.cn/yunye/axios/234845 || https://www.npmjs.com/search? ...
- [na]timewait优化
解决timewait 加入一条socket配置,重用ip和端口 phone=socket(AF_INET,SOCK_STREAM) phone.setsockopt(SOL_SOCKET,SO_REU ...
- git更新代码报错,error: The following untracked working tree files would be overwritten by ch
git忽略大小写导致的, git config --add core.ignorecase true
- Py之np.concatenate函数【转载】
转自:https://docs.scipy.org/doc/numpy/reference/generated/numpy.concatenate.html 1.nupmy.concatenate函数 ...
- PostgreSQL远程连接配置
postgresql默认情况下,远程访问不能成功,如果需要允许远程访问,需要修改两个配置文件,说明如下: 1.postgresql.conf 将该文件中的listen_addresses项值设定为“* ...
- mysql普通查询日志
- rpgmakermv(6) YEP_ItemSynthesis.js物品合成插件
物品合成插件. 用途?太多了呢. 低级宝石合成高级,还有装备,武器,药水等等. ============================================================ ...
- 匹克定理pick
与POJ1226为例 要知道在一个格点多边形内 知道期内部的点数 Q,边上的点数L,就可以知道他的面积pick定理及 S=Q+L/2-1; 然后 还有边上的点数除了多边形的顶点外,还有一些点该怎么求呢 ...
- MacaW Baby Learns Computer
A - Macaw Baby Learns Computer Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & ...
- Python:键盘输入input
从键盘读入数据 >>> num=input('利润是:') 利润是:55 >>>