EF三层构架+接口实现(增删改查)
DbContext
DbContext是EntityFramework很重要的部分,连接域模型与数据库的桥梁,是与数据库通信的主要类。
DbContext主要负责以下活动:
EntitySet::DbContext包含了所有映射到表的entities
Querying:将Linq-To-Entities转译为Sql并发送到数据库
Change Tracking:从数据库获取entities后保留并跟踪实体数据变化
Persisting Data:根据entity状态执行Insert、update、delete命令
Caching:DbContext的默认第一级缓存,在上下文中的生命周期中存储entity
Manage Relationship:DbContext在DbFirst模式中使用CSDL、MSL、SSDL管理对象关系,Code first中使用fluent api 管理关系
增删改查操作
IDAL
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace ADO.NETEFDemo { public interface IDAL<T> where T : class,new() { /// <summary> /// 增 /// </summary> /// <param name="model"></param> /// <returns></returns> int Add(T model); /// <summary> /// 删 /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> int Delete(Expression<Func<T, bool>> whereLambda); /// <summary> /// 改 /// </summary> /// <param name="whereLambda"></param> /// <param name="propertyNames"></param> /// <param name="perpertyValues"></param> /// <returns></returns> int Update(Expression<Func<T, bool>> whereLambda, string[] propertyNames, object[] perpertyValues); /// <summary> /// 查 /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> List<T> GetModelList(Expression<Func<T, bool>> whereLambda); } }
DAL
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace ADO.NETEFDemo { public class DAL<T> : IDAL<T> where T : class,new() { /// <summary> /// 增 /// </summary> /// <param name="model"></param> /// <returns></returns> public int Add(T model) { using (testbakEntities db = new testbakEntities()) { db.Set<T>().Add(model); return db.SaveChanges(); } } /// <summary> /// 删 /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> public int Delete(Expression<Func<T, bool>> whereLambda) { using (testbakEntities db = new testbakEntities()) { var dbQuery = db.Set<T>(); //先查询 对应表的 集合 var list = dbQuery.Where(whereLambda).ToList(); //遍历集合 里要删除的元素 foreach (var item in list) { //标记为 删除状态 dbQuery.Remove(item); } return db.SaveChanges(); } } /// <summary> /// 改 /// </summary> /// <param name="whereLambda"></param> /// <param name="propertyNames"></param> /// <param name="perpertyValues"></param> /// <returns></returns> public int Update(Expression<Func<T, bool>> whereLambda, string[] propertyNames, object[] perpertyValues) { using (testbakEntities db = new testbakEntities()) { //1、查询要修改的对象集合 var list = db.Set<T>().Where<T>(whereLambda).ToList(); //2、获取要修改的对象的类型 Type t = typeof(T); //3、循环要修改的实体对象,并根据要修改的属性名修改对象对应的属性值 foreach (var item in list) { //循环 要修改的属性 名称, 并 反射取出 t 中的 属性对象 for (int index = 0; index < propertyNames.Length; index++) { //获取要修改的属性名 string pName = propertyNames[index]; //获取属性对象 PropertyInfo pi = t.GetProperty(pName); //调用属性对象的 SetValue方法 为当前循环的 item对象 对应的属性赋值 pi.SetValue(item, perpertyValues[index], null); } } return db.SaveChanges(); } } /// <summary> /// 查 /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> public List<T> GetModelList(Expression<Func<T, bool>> whereLambda) { using (testbakEntities db = new testbakEntities()) { return db.Set<T>().Where(whereLambda).ToList(); } } } }
BLL
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace ADO.NETEFDemo { public static class BLL<T> where T : class,new() { private static IDAL<T> dal = new DAL<T>(); /// <summary> /// 新增 /// </summary> /// <param name="model"></param> public static int Add(T model) { return dal.Add(model); } /// <summary> /// 删除 /// </summary> /// <param name="whereLambda"></param> public static int Delete(Expression<Func<T, bool>> whereLambda) { return dal.Delete(whereLambda); } /// <summary> /// 修改 /// </summary> /// <param name="whereLambda"></param> /// <param name="propertyNames"></param> /// <param name="perpertyValues"></param> /// <returns></returns> public static int Update(Expression<Func<T, bool>> whereLambda, string[] propertyNames, object[] perpertyValues) { return dal.Update(whereLambda, propertyNames, perpertyValues); } /// <summary> /// 查询 /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> public static List<T> GetModelList(Expression<Func<T, bool>> whereLambda) { return dal.GetModelList(whereLambda); } } }
调用
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ADO.NETEFDemo { class Program { static void Main(string[] args) { GetArticleList(); AddArticle(); GetArticleList(); UpdateArticle(); GetArticleList(); DeleteArticel(); GetArticleList(); Console.ReadKey(); } /// <summary> /// 更新 /// </summary> private static void UpdateArticle() { int result = BLL<t_crobot_reship_articles> .Update(e => e.title.Contains("EF"), new[] { "title", "update_time" }, new object[] { "我是使用EF修改过标题的文章", DateTime.Now }); if (result >= 0) { Console.WriteLine("更新成功"); } else { Console.WriteLine("更新失败"); } Console.WriteLine(); } /// <summary> /// 删除 /// </summary> private static void DeleteArticel() { int result = BLL<t_crobot_reship_articles>.Delete(e => e.title.Contains("EF")); if (result >= 0) { Console.WriteLine("删除成功"); } else { Console.WriteLine("删除失败"); } Console.WriteLine(); } /// <summary> /// 新增 /// </summary> private static void AddArticle() { t_crobot_reship_articles model = new t_crobot_reship_articles(); model.create_time = DateTime.Now; model.module_id = 1; model.adword_id = 20; model.pick_id = 1; model.vote_id = "1"; model.title = "我是使用EF添加的文章"; model.content_id = 1; model.release_url = "http://www.sss.com"; model.state = true; int result = BLL<t_crobot_reship_articles>.Add(model); if (result >= 0) { Console.WriteLine("新增成功"); } else { Console.WriteLine("新增失败"); } Console.WriteLine(); } /// <summary> /// 获取文章列表 /// </summary> private static void GetArticleList() { List<t_crobot_reship_articles> articleList = BLL<t_crobot_reship_articles> .GetModelList(e => e.state == true); Console.WriteLine("文章总数:" + articleList.Count.ToString()); foreach (t_crobot_reship_articles model in articleList) { Console.WriteLine("标题:" + model.title); } Console.WriteLine(); } } }