ExecuteSql.cs 866 B

12345678910111213141516171819202122232425262728293031323334353637
  1. using Microsoft.EntityFrameworkCore;
  2. using VberAdmin.EntityFrameworkCore;
  3. namespace VberAdmin.Seed;
  4. public static class ExecuteSql
  5. {
  6. public static void TruncateTable(this VberAdminDbContext context, string tableName)
  7. {
  8. if (string.IsNullOrEmpty(tableName))
  9. {
  10. return;
  11. }
  12. var sql = $"TRUNCATE TABLE {tableName}";
  13. context.Sql(sql);
  14. }
  15. public static void DeleteTable(this VberAdminDbContext context, string tableName)
  16. {
  17. if (string.IsNullOrEmpty(tableName))
  18. {
  19. return;
  20. }
  21. var sql = $"DELETE FROM {tableName}";
  22. context.Sql(sql);
  23. }
  24. public static void Sql(this VberAdminDbContext context, string sql)
  25. {
  26. if (string.IsNullOrEmpty(sql))
  27. {
  28. return;
  29. }
  30. context.Database.ExecuteSqlRaw(sql);
  31. }
  32. }