Muse.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Data.Entity.Infrastructure;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. using System.Text;
  8. namespace Azylee.DB.SQLite.Engine
  9. {
  10. public class Muse : IDBContext
  11. {
  12. public SuperDB Context;
  13. public Muse()
  14. {
  15. Context = new SuperDB();
  16. }
  17. public int Add<T>(T EntityObj, bool isSave = true) where T : class
  18. {
  19. try
  20. {
  21. this.Context.Set<T>().Add(EntityObj);
  22. if (isSave)
  23. {
  24. return Save();
  25. }
  26. }
  27. catch (Exception e) { }
  28. return 0;
  29. }
  30. public int Adds<T>(IEnumerable<T> EntityObjs) where T : class
  31. {
  32. try
  33. {
  34. Context.Set<T>().AddRange(EntityObjs);
  35. return Save();
  36. }
  37. catch (Exception e)
  38. {
  39. return 0;
  40. }
  41. }
  42. public int Del<T>(T EntityObj, bool isSave) where T : class
  43. {
  44. try
  45. {
  46. this.Context.Set<T>().Remove(EntityObj);
  47. if (isSave)
  48. {
  49. return Save();
  50. }
  51. }
  52. catch (Exception e) { }
  53. return 0;
  54. }
  55. public int Dels<T>(IEnumerable<T> EntityObjs) where T : class
  56. {
  57. try
  58. {
  59. this.Context.Set<T>().RemoveRange(EntityObjs);
  60. return Save();
  61. }
  62. catch (Exception e) { }
  63. return 0;
  64. }
  65. public int Update<T>(T EntityObj, bool isSave) where T : class
  66. {
  67. try
  68. {
  69. this.Context.Entry(EntityObj).State = EntityState.Modified;
  70. if (isSave)
  71. {
  72. return Save();
  73. }
  74. }
  75. catch (Exception e) { }
  76. return 0;
  77. }
  78. public int Save()
  79. {
  80. return Context.SaveChanges();
  81. }
  82. public T Get<T>(Expression<Func<T, bool>> expression, string[] include) where T : class
  83. {
  84. try
  85. {
  86. if (include != null && include.Count() > 0)
  87. {
  88. DbQuery<T> query = GetInclude<T>(include);
  89. if (query != null)
  90. return query.FirstOrDefault(expression);
  91. }
  92. return this.Context.Set<T>().FirstOrDefault(expression);
  93. }
  94. catch (Exception e)
  95. {
  96. }
  97. return null;
  98. }
  99. public IEnumerable<T> Gets<T>(Expression<Func<T, bool>> expression, string[] include) where T : class
  100. {
  101. try
  102. {
  103. if (include != null && include.Count() > 0)
  104. {
  105. DbQuery<T> query = GetInclude<T>(include);
  106. if (query != null)
  107. return query.Where(expression).ToList();
  108. }
  109. }
  110. catch (Exception)
  111. {
  112. throw;
  113. }
  114. return Context.Set<T>().Where(expression).ToList();
  115. }
  116. public IEnumerable<T> GetAll<T>(string[] include, bool track) where T : class
  117. {
  118. if (include != null && include.Count() > 0)
  119. {
  120. DbQuery<T> query = GetInclude<T>(include);
  121. if (query != null)
  122. if (track)
  123. return query.ToList();
  124. else
  125. return query.AsNoTracking().ToList();
  126. }
  127. if (!track)
  128. Context.Set<T>().AsNoTracking().ToList();
  129. return Context.Set<T>().ToList();
  130. }
  131. private DbQuery<T> GetInclude<T>(string[] include) where T : class
  132. {
  133. DbQuery<T> searchCondition = null;
  134. foreach (var item in include)
  135. {
  136. if (searchCondition == null)
  137. searchCondition = this.Context.Set<T>().Include(item);
  138. else
  139. searchCondition = searchCondition.Include(item);
  140. }
  141. return searchCondition;
  142. }
  143. public bool Any<T>(Expression<Func<T, bool>> expression, string[] include) where T : class
  144. {
  145. try
  146. {
  147. if (include != null && include.Count() > 0)
  148. {
  149. DbQuery<T> query = GetInclude<T>(include);
  150. if (query != null)
  151. return query.AsNoTracking().Any(expression);
  152. }
  153. return this.Context.Set<T>().AsNoTracking().Any(expression);
  154. }
  155. catch (Exception e)
  156. {
  157. }
  158. return false;
  159. }
  160. public DbSet<T> Do<T>() where T : class
  161. {
  162. return Context.Set<T>();
  163. }
  164. public IEnumerable<T> ExecuteSqlCom<T, U>(string sql, U paramObjs)
  165. where U : class
  166. where T : class
  167. {
  168. return Context.Set<T>().SqlQuery(sql, paramObjs);
  169. }
  170. public void Dispose()
  171. {
  172. Context.Dispose();
  173. }
  174. }
  175. }