Muse.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 bool 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 false;
  30. }
  31. public bool Del<T>(T EntityObj, bool isSave) where T : class
  32. {
  33. try
  34. {
  35. this.db.Set<T>().Remove(EntityObj);
  36. if (isSave)
  37. {
  38. return Save();
  39. }
  40. return false;
  41. }
  42. catch (Exception e) { }
  43. return false;
  44. }
  45. public bool Update<T>(T EntityObj, bool isSave) where T : class
  46. {
  47. try
  48. {
  49. this.db.Entry(EntityObj).State = EntityState.Modified;
  50. if (isSave)
  51. {
  52. return Save();
  53. }
  54. return false;
  55. }
  56. catch (Exception e) { }
  57. return false;
  58. }
  59. public bool Save()
  60. {
  61. return db.SaveChanges() > 0;
  62. }
  63. public T Get<T>(Expression<Func<T, bool>> expression, string[] include) where T : class
  64. {
  65. try
  66. {
  67. if (include != null && include.Count() > 0)
  68. {
  69. DbQuery<T> query = GetInclude<T>(include);
  70. if (query != null)
  71. return query.FirstOrDefault(expression);
  72. }
  73. return this.db.Set<T>().FirstOrDefault(expression);
  74. }
  75. catch (Exception e)
  76. {
  77. }
  78. return null;
  79. }
  80. public IEnumerable<T> Gets<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.Where(expression).ToList();
  89. }
  90. }
  91. catch (Exception)
  92. {
  93. throw;
  94. }
  95. return db.Set<T>().Where(expression).ToList();
  96. }
  97. public IEnumerable<T> GetAll<T>(string[] include, bool track) where T : class
  98. {
  99. if (include != null && include.Count() > 0)
  100. {
  101. DbQuery<T> query = GetInclude<T>(include);
  102. if (query != null)
  103. if (track)
  104. return query.ToList();
  105. else
  106. return query.AsNoTracking().ToList();
  107. }
  108. if (!track)
  109. db.Set<T>().AsNoTracking().ToList();
  110. return db.Set<T>().ToList();
  111. }
  112. private DbQuery<T> GetInclude<T>(string[] include) where T : class
  113. {
  114. DbQuery<T> searchCondition = null;
  115. foreach (var item in include)
  116. {
  117. if (searchCondition == null)
  118. searchCondition = this.db.Set<T>().Include(item);
  119. else
  120. searchCondition = searchCondition.Include(item);
  121. }
  122. return searchCondition;
  123. }
  124. public bool Any<T>(Expression<Func<T, bool>> expression, string[] include) where T : class
  125. {
  126. try
  127. {
  128. if (include != null && include.Count() > 0)
  129. {
  130. DbQuery<T> query = GetInclude<T>(include);
  131. if (query != null)
  132. return query.Any(expression);
  133. }
  134. return this.db.Set<T>().Any(expression);
  135. }
  136. catch (Exception e)
  137. {
  138. }
  139. return false;
  140. }
  141. public DbSet<T> Do<T>() where T : class
  142. {
  143. return db.Set<T>();
  144. }
  145. public void Dispose()
  146. {
  147. db.Dispose();
  148. }
  149. public IEnumerable<T> ExecuteSqlCom<T, U>(string sql, U paramObjs)
  150. where U : class
  151. where T : class
  152. {
  153. return db.Set<T>().SqlQuery(sql, paramObjs);
  154. }
  155. }
  156. }