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