CommonDAL封装:
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Linq.Expressions; using System.Text; using YX.Model; namespace YX.BLL { /// <summary> /// 基础 dal 类 T 对应数据泛型 /// </summary> public class CommonDAL<T> : IDisposable where T : class { /// <summary> /// 当前上下文 /// </summary> DbContext DB; #region 构造函数 public CommonDAL() { //默认db为mclentities this.DB = DBHelper.CreateDB(ConnectionStr.ServiceEntities); } public CommonDAL(string connName) { this.DB = DBHelper.CreateDB(connName); } public CommonDAL(DbContext db) { if (db == null) { throw new ArgumentNullException("db"); } this.DB = db; } #endregion /// <summary> /// 当前db /// </summary> public DbContext CurrDB { get { return this.DB; } set { this.DB = value; } } /// <summary> /// 添加 /// </summary> /// <param name="t">数据对象</param> /// <returns>成功否</returns> public bool Add(T t) { try { t = DB.Set<T>().Add(t); return DB.SaveChanges() > 0; } catch (Exception ex) { //添加错误日志 Log.Logger.AddLog(ex.Message, ex); throw new Exception(ex.Message, ex); } } /// <summary> /// 更新 /// </summary> /// <param name="t">数据对象</param> /// <returns>成功否</returns> public bool Update(T t) { try { this.DB.Entry<T>(t).State = EntityState.Modified; return this.DB.SaveChanges() > 0; } catch (Exception ex) { //添加错误日志 Log.Logger.AddLog(ex.Message, ex); throw new Exception(ex.Message, ex); } } /// <summary> /// 删除 /// </summary> /// <param name="t">数据对象</param> /// <returns>成功否</returns> public bool Delete(T t) { try { t = DB.Set<T>().Remove(t); return DB.SaveChanges() > 0; } catch (Exception ex) { //添加错误日志 Log.Logger.AddLog(ex.Message, ex); throw new Exception(ex.Message, ex); } } /// <summary> /// 返回query /// </summary> /// <returns>查询接口</returns> public IQueryable<T> GetQuery() { var query = DB.Set<T>(); return query; } /// <summary> /// 根据主键查询 /// </summary> /// <param name="id"></param> /// <returns></returns> //public abstract T Get(Key id); /// <summary> /// 返回所有值 /// </summary> /// <returns></returns> public List<T> GetAll() { var dbQuery = this.GetQuery(); return dbQuery.ToList(); } /// <summary> /// 释放 /// </summary> public void Dispose() { this.CurrDB.Dispose(); } } }
DBHelper:
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; namespace YX.BLL { /// <summary> /// 数据上下文创建类 /// </summary> public class DBHelper { /// <summary> /// 创建上下文db方法 /// </summary> /// <param name="connName"></param> /// <returns></returns> public static DbContext CreateDB(string connName) { return new DbContext(connName); } } }
接下来就是在BaseBLL中去调用DAL:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading;using YX.Entitys; using YX.Model.SZR; using YX.Log; using System.Reflection; namespace YX.BLL.ShenZhouRong { public class SZRBaseBLL : BaseBLL { /// <summary> /// 数据操作类 /// </summary> public CommonDAL<TB_SZR_InterfaceLog> dal; private string _AppID = null; /// <summary> /// 当前APPID /// </summary> public string CurrentAppID { get { return this._AppID; } set { this._AppID = value; } } /// <summary> /// 构造函数 /// </summary> public SZRBaseBLL() { dal = new CommonDAL<TB_SZR_InterfaceLog>(); } /// <summary> /// 按 ID 获取 /// </summary> /// <param name="id">id</param> /// <returns>数据对象</returns> public TB_SZR_InterfaceLog GetSingle(int id) { return dal.GetQuery().Where(s => s.Id == id).FirstOrDefault(); } public void Add() {
Model_11 tss = new Model_11
{
CreateDate = DateTime.Now,
ERROR_CODE = ERROR_CODE,
IDNO = IDNO,
NAME = NAME,
RESULT = RESULT
};
CommonDAL<Model_11> dal = new CommonDAL<Model_11>();
return dal.Add(tss);
} } }
时间: 2024-10-09 22:46:18