| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- 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>(T EntityObj, bool isSave = true) where T : class
- {
- try
- {
- this.Context.Set<T>().Add(EntityObj);
- if (isSave)
- {
- return Save();
- }
- }
- catch (Exception e) { }
- return 0;
- }
- public int Adds<T>(IEnumerable<T> EntityObjs) where T : class
- {
- try
- {
- Context.Set<T>().AddRange(EntityObjs);
- return Save();
- }
- catch (Exception e)
- {
- return 0;
- }
- }
- public int Del<T>(T EntityObj, bool isSave) where T : class
- {
- try
- {
- this.Context.Set<T>().Remove(EntityObj);
- if (isSave)
- {
- return Save();
- }
- }
- catch (Exception e) { }
- return 0;
- }
- public int Dels<T>(IEnumerable<T> EntityObjs) where T : class
- {
- try
- {
- this.Context.Set<T>().RemoveRange(EntityObjs);
- return Save();
- }
- catch (Exception e) { }
- return 0;
- }
- public int Update<T>(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<T>(Expression<Func<T, bool>> expression, string[] include) where T : class
- {
- try
- {
- if (include != null && include.Count() > 0)
- {
- DbQuery<T> query = GetInclude<T>(include);
- if (query != null)
- return query.FirstOrDefault(expression);
- }
- return this.Context.Set<T>().FirstOrDefault(expression);
- }
- catch (Exception e)
- {
- }
- return null;
- }
- public IEnumerable<T> Gets<T>(Expression<Func<T, bool>> expression, string[] include) where T : class
- {
- try
- {
- if (include != null && include.Count() > 0)
- {
- DbQuery<T> query = GetInclude<T>(include);
- if (query != null)
- return query.Where(expression).ToList();
- }
- }
- catch (Exception)
- {
- throw;
- }
- return Context.Set<T>().Where(expression).ToList();
- }
- public IEnumerable<T> GetAll<T>(string[] include, bool track) where T : class
- {
- if (include != null && include.Count() > 0)
- {
- DbQuery<T> query = GetInclude<T>(include);
- if (query != null)
- if (track)
- return query.ToList();
- else
- return query.AsNoTracking().ToList();
- }
- if (!track)
- Context.Set<T>().AsNoTracking().ToList();
- return Context.Set<T>().ToList();
- }
- private DbQuery<T> GetInclude<T>(string[] include) where T : class
- {
- DbQuery<T> searchCondition = null;
- foreach (var item in include)
- {
- if (searchCondition == null)
- searchCondition = this.Context.Set<T>().Include(item);
- else
- searchCondition = searchCondition.Include(item);
- }
- return searchCondition;
- }
- public bool Any<T>(Expression<Func<T, bool>> expression, string[] include) where T : class
- {
- try
- {
- if (include != null && include.Count() > 0)
- {
- DbQuery<T> query = GetInclude<T>(include);
- if (query != null)
- return query.AsNoTracking().Any(expression);
- }
- return this.Context.Set<T>().AsNoTracking().Any(expression);
- }
- catch (Exception e)
- {
- }
- return false;
- }
- public DbSet<T> Do<T>() where T : class
- {
- return Context.Set<T>();
- }
- public IEnumerable<T> ExecuteSqlCom<T, U>(string sql, U paramObjs)
- where U : class
- where T : class
- {
- return Context.Set<T>().SqlQuery(sql, paramObjs);
- }
- public void Dispose()
- {
- Context.Dispose();
- }
- }
- }
|