using Cowain.Bake.Model; using MySql.Data.MySqlClient; using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Linq.Expressions; using Unity; namespace Cowain.Bake.BLL { public class ServiceBase { public readonly object _lockObj = new object(); protected IUnityContainer _unityContainer; public ServiceBase(IUnityContainer unityContainer) { //using (var context = new DbContext()) // 创建DbContext //第二层:DbConnection(由ADO.NET连接池管理);password=123456;Pooling=true;Max Pool Size=100; //var data = context.Products.ToList(); // 触发连接获取 _unityContainer = unityContainer; } //public int Commit() //{ // return this.Context.SaveChanges(); //} public int Delete(int Id) where T : class { using (var Context = new BakingEntities()) { T t = Context.Set().Find(Id);//也可以附加 if (t == null) throw new Exception("t is null"); Context.Set().Remove(t); return Context.SaveChanges(); } } public int Delete(T t) where T : class { using (var Context = new BakingEntities()) { if (t == null) throw new Exception("t is null"); Context.Set().Attach(t); Context.Set().Remove(t); return Context.SaveChanges(); } } public void Delete(IEnumerable tList) where T : class { using (var Context = new BakingEntities()) { foreach (var t in tList) { Context.Set().Attach(t); } Context.Set().RemoveRange(tList); Context.SaveChanges(); } } public List Find() where T : class { using (var Context = new BakingEntities()) { return Context.Set().ToList(); } } public T Find(int id) where T : class { using (var Context = new BakingEntities()) { return Context.Set().Find(id); } } public int Insert(T t) where T : class { using (var Context = new BakingEntities()) { Context.Set().Add(t); return Context.SaveChanges(); } } public int Insert(IEnumerable tList) where T : class { using (var Context = new BakingEntities()) { Context.Set().AddRange(tList); return Context.SaveChanges(); } } public IQueryable Query(Expression> funcWhere) where T : class { using (var Context = new BakingEntities()) { return Context.Set().Where(funcWhere); } } public int Update(T t) where T : class { if (t == null) throw new Exception("t is null"); using (var Context = new BakingEntities()) { Context.Set().Attach(t);//将数据附加到上下文,支持实体修改和新实体,重置为UnChanged Context.Entry(t).State = EntityState.Modified; return Context.SaveChanges(); } } public void Update(IEnumerable tList) where T : class { using (var Context = new BakingEntities()) { foreach (var t in tList) { Context.Set().Attach(t); Context.Entry(t).State = EntityState.Modified; } Context.SaveChanges(); } } public int UpdateListParas(IEnumerable tList) where T : class { using (var Context = new BakingEntities()) { foreach (var t in tList) { Context.Set().Attach(t); Context.Entry(t).State = EntityState.Modified; } return Context.SaveChanges(); } } public virtual void Dispose() { //if (this.Context != null) //{ // this.Context.Dispose(); //} } public DataTable GetDataTable(string sql) { lock(_lockObj) { MySqlConnection conn = new MySqlConnection(); using (var Context = new BakingEntities()) { conn.ConnectionString = Context.Database.Connection.ConnectionString; } if (conn.State != ConnectionState.Open) { conn.Open(); } MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; cmd.CommandText = sql; MySqlDataAdapter adapter = new MySqlDataAdapter(cmd); DataTable table = new DataTable(); adapter.Fill(table); conn.Close();//连接需要关闭 conn.Dispose(); return table; } } } }