DbContextExtension.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Data;
  7. using System.Data.Common;
  8. using System.Reflection;
  9. using Microsoft.EntityFrameworkCore;
  10. using Microsoft.EntityFrameworkCore.Metadata;
  11. using Microsoft.EntityFrameworkCore.ChangeTracking;
  12. using YiSha.Util;
  13. namespace YiSha.Data.EF
  14. {
  15. public static class DbContextExtension
  16. {
  17. /// <summary>
  18. /// 拼接删除SQL语句
  19. /// </summary>
  20. /// <param name="tableName">表名</param>
  21. /// <returns></returns>
  22. public static string DeleteSql(string tableName)
  23. {
  24. StringBuilder strSql = new StringBuilder("DELETE FROM " + tableName + "");
  25. return strSql.ToString();
  26. }
  27. /// <summary>
  28. /// 拼接删除SQL语句
  29. /// </summary>
  30. /// <param name="tableName">表名</param>
  31. /// <param name="propertyName">实体属性名称</param>
  32. /// <param name="propertyValue">字段值:数组1,2,3,4,5,6.....</param>
  33. /// <returns></returns>
  34. public static string DeleteSql(string tableName, string propertyName, string propertyValue)
  35. {
  36. StringBuilder strSql = new StringBuilder("DELETE FROM " + tableName + " WHERE " + propertyName + " = " + propertyValue + "");
  37. return strSql.ToString();
  38. }
  39. /// <summary>
  40. /// 拼接批量删除SQL语句
  41. /// </summary>
  42. /// <param name="tableName">表名</param>
  43. /// <param name="propertyName">实体属性名称</param>
  44. /// <param name="propertyValue">字段值:数组1,2,3,4,5,6.....</param>
  45. /// <returns></returns>
  46. public static string DeleteSql(string tableName, string propertyName, string[] propertyValue)
  47. {
  48. string strSql = "DELETE FROM " + tableName + " WHERE " + propertyName + " IN (" + string.Join(",", propertyValue) + ")";
  49. return strSql.ToString();
  50. }
  51. /// <summary>
  52. /// 获取实体映射对象
  53. /// </summary>
  54. /// <typeparam name="T"></typeparam>
  55. /// <param name="dbcontext"></param>
  56. /// <returns></returns>
  57. public static IEntityType GetEntityType<T>(DbContext dbcontext) where T : class
  58. {
  59. return dbcontext.Model.FindEntityType(typeof(T));
  60. }
  61. /// <summary>
  62. /// 存储过程语句
  63. /// </summary>
  64. /// <param name="procName">存储过程名称</param>
  65. /// <param name="dbParameter">执行命令所需的sql语句对应参数</param>
  66. /// <returns></returns>
  67. public static string BuilderProc(string procName, params DbParameter[] dbParameter)
  68. {
  69. StringBuilder strSql = new StringBuilder("exec " + procName);
  70. if (dbParameter != null)
  71. {
  72. foreach (var item in dbParameter)
  73. {
  74. strSql.Append(" " + item + ",");
  75. }
  76. strSql = strSql.Remove(strSql.Length - 1, 1);
  77. }
  78. return strSql.ToString();
  79. }
  80. public static void SetEntityDefaultValue(DbContext dbcontext)
  81. {
  82. foreach (EntityEntry entry in dbcontext.ChangeTracker.Entries().Where(p => p.State == EntityState.Added))
  83. {
  84. #region 把null设置成对应属性类型的默认值
  85. Type type = entry.Entity.GetType();
  86. PropertyInfo[] props = ReflectionHelper.GetProperties(type);
  87. foreach (PropertyInfo prop in props)
  88. {
  89. object value = prop.GetValue(entry.Entity, null);
  90. if (value == null)
  91. {
  92. string sType = string.Empty;
  93. if (prop.PropertyType.GenericTypeArguments.Length > 0)
  94. {
  95. sType = prop.PropertyType.GenericTypeArguments[0].Name;
  96. }
  97. else
  98. {
  99. sType = prop.PropertyType.Name;
  100. }
  101. switch (sType)
  102. {
  103. case "Boolean":
  104. prop.SetValue(entry.Entity, false);
  105. break;
  106. case "Char":
  107. prop.SetValue(entry.Entity, 0);
  108. break;
  109. case "SByte":
  110. prop.SetValue(entry.Entity, 0);
  111. break;
  112. case "Byte":
  113. prop.SetValue(entry.Entity, 0);
  114. break;
  115. case "Int16":
  116. prop.SetValue(entry.Entity, 0);
  117. break;
  118. case "UInt16":
  119. prop.SetValue(entry.Entity, 0);
  120. break;
  121. case "Int32":
  122. prop.SetValue(entry.Entity, 0);
  123. break;
  124. case "UInt32":
  125. prop.SetValue(entry.Entity, 0);
  126. break;
  127. case "Int64":
  128. prop.SetValue(entry.Entity, (Int64)0);
  129. break;
  130. case "UInt64":
  131. prop.SetValue(entry.Entity, 0);
  132. break;
  133. case "Single":
  134. prop.SetValue(entry.Entity, 0);
  135. break;
  136. case "Double":
  137. prop.SetValue(entry.Entity, 0);
  138. break;
  139. case "Decimal":
  140. prop.SetValue(entry.Entity, (decimal)0);
  141. break;
  142. case "DateTime":
  143. prop.SetValue(entry.Entity, GlobalConstant.DefaultTime);
  144. break;
  145. case "String":
  146. prop.SetValue(entry.Entity, string.Empty);
  147. break;
  148. default: break;
  149. }
  150. }
  151. else if (value.ToString() == DateTime.MinValue.ToString())
  152. {
  153. // sql server datetime类型的的范围不到0001-01-01,所以转成1970-01-01
  154. prop.SetValue(entry.Entity, GlobalConstant.DefaultTime);
  155. }
  156. }
  157. #endregion
  158. }
  159. }
  160. }
  161. }