using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Linq; using System.Linq.Expressions; using System.Text; namespace Azylee.DB.SQLite.Engine { public class Muse : IDBContext { public SuperDB Context; public Muse() { Context = new SuperDB(); } public int Add(T EntityObj, bool isSave = true) where T : class { try { this.Context.Set().Add(EntityObj); if (isSave) { return Save(); } } catch (Exception e) { } return 0; } public int Adds(IEnumerable EntityObjs) where T : class { try { Context.Set().AddRange(EntityObjs); return Save(); } catch (Exception e) { return 0; } } public int Del(T EntityObj, bool isSave) where T : class { try { this.Context.Set().Remove(EntityObj); if (isSave) { return Save(); } } catch (Exception e) { } return 0; } public int Dels(IEnumerable EntityObjs) where T : class { try { this.Context.Set().RemoveRange(EntityObjs); return Save(); } catch (Exception e) { } return 0; } public int Update(T EntityObj, bool isSave) where T : class { try { this.Context.Entry(EntityObj).State = EntityState.Modified; if (isSave) { return Save(); } } catch (Exception e) { } return 0; } public int Save() { return Context.SaveChanges(); } public T Get(Expression> expression, string[] include) where T : class { try { if (include != null && include.Count() > 0) { DbQuery query = GetInclude(include); if (query != null) return query.FirstOrDefault(expression); } return this.Context.Set().FirstOrDefault(expression); } catch (Exception e) { } return null; } public IEnumerable Gets(Expression> expression, string[] include) where T : class { try { if (include != null && include.Count() > 0) { DbQuery query = GetInclude(include); if (query != null) return query.Where(expression).ToList(); } } catch (Exception) { throw; } return Context.Set().Where(expression).ToList(); } public IEnumerable GetAll(string[] include, bool track) where T : class { if (include != null && include.Count() > 0) { DbQuery query = GetInclude(include); if (query != null) if (track) return query.ToList(); else return query.AsNoTracking().ToList(); } if (!track) Context.Set().AsNoTracking().ToList(); return Context.Set().ToList(); } private DbQuery GetInclude(string[] include) where T : class { DbQuery searchCondition = null; foreach (var item in include) { if (searchCondition == null) searchCondition = this.Context.Set().Include(item); else searchCondition = searchCondition.Include(item); } return searchCondition; } public bool Any(Expression> expression, string[] include) where T : class { try { if (include != null && include.Count() > 0) { DbQuery query = GetInclude(include); if (query != null) return query.AsNoTracking().Any(expression); } return this.Context.Set().AsNoTracking().Any(expression); } catch (Exception e) { } return false; } public DbSet Do() where T : class { return Context.Set(); } public IEnumerable ExecuteSqlCom(string sql, U paramObjs) where U : class where T : class { return Context.Set().SqlQuery(sql, paramObjs); } public void Dispose() { Context.Dispose(); } } }