using Cowain.Preheat.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.Preheat.BLL { public class ServiceBase { public readonly object _lockObj = new object(); //protected DbContext Context { get; private set; } protected IUnityContainer _unityContainer; public ServiceBase(IUnityContainer unityContainer) { //Context = new PreheatEntities(); _unityContainer = unityContainer; } //public int Commit() //{ // return this.Context.SaveChanges(); //} public int Delete(int Id) where T : class { using (var Context = new PreheatEntities()) { 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 PreheatEntities()) { 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 PreheatEntities()) { 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 PreheatEntities()) { return Context.Set().ToList(); } } public T Find(int id) where T : class { using (var Context = new PreheatEntities()) { return Context.Set().Find(id); } } public int Insert(T t) where T : class { using (var Context = new PreheatEntities()) { Context.Set().Add(t); return Context.SaveChanges(); } } public int Insert(IEnumerable tList) where T : class { using (var Context = new PreheatEntities()) { Context.Set().AddRange(tList); return Context.SaveChanges();//写在这里 就不需要单独commit 不写就需要 } } public IQueryable Query(Expression> funcWhere) where T : class { using (var Context = new PreheatEntities()) { return Context.Set().Where(funcWhere); } } public int Update(T t) where T : class { using (var Context = new PreheatEntities()) { if (t == null) throw new Exception("t is null"); Context.Set().Attach(t);//将数据附加到上下文,支持实体修改和新实体,重置为UnChanged Context.Entry(t).State = EntityState.Modified; return Context.SaveChanges(); } } public bool Update(IEnumerable tList) where T : class { using (var Context = new PreheatEntities()) { foreach (var t in tList) { Context.Set().Attach(t); Context.Entry(t).State = EntityState.Modified; } return Context.SaveChanges()> 0 ? true : false; } } public int UpdateListParas(IEnumerable tList) where T : class { using (var Context = new PreheatEntities()) { 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 int ExecuteNonQuery(string sql) { using (var Context = new PreheatEntities()) { return Context.Database.ExecuteSqlCommand(sql); } } public DataTable GetDataTable(string sql) { lock(_lockObj) { using (var Context = new PreheatEntities()) { MySqlConnection conn = new MySqlConnection(); 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; } } } } }