EF三層構架+介面實現(增刪改查)
阿新 • • 發佈:2018-12-12
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(); } } }