Muse.cs 5.1 KB

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