DbClearCmd.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using Microsoft.Data.SqlClient;
  2. using Quadarax.Application.QLiberace.Base;
  3. using Quadarax.Application.QLiberace.Console.Commands.Base;
  4. using Quadarax.Foundation.Core.QConsole;
  5. using Quadarax.Foundation.Core.QConsole.Attributes;
  6. using Quadarax.Foundation.Core.Value;
  7. namespace Quadarax.Application.QLiberace.Console.Commands
  8. {
  9. [CommandDefinition]
  10. internal class DbClearCmd : DbContextCommand
  11. {
  12. #region *** Properties ***
  13. public override string Name => Constants.Commands.DbClear.Name;
  14. public override string Description => Constants.Commands.DbClear.Description;
  15. #endregion
  16. #region *** Fields ***
  17. #endregion
  18. #region *** Constructor ***
  19. public DbClearCmd(Engine engine) : base(engine)
  20. {
  21. }
  22. #endregion
  23. #region *** EXECUTE ***
  24. protected override Result OnExecute(QlbrcDbContext context)
  25. {
  26. WriteInfo("Connecting database...");
  27. var resultCode = 0;
  28. try
  29. {
  30. if (context.Database.CanConnect())
  31. {
  32. WriteInfo("Deleting database...");
  33. context.Database.EnsureDeleted();
  34. }
  35. WriteInfo("Creating database...");
  36. context.Database.EnsureCreated();
  37. }
  38. catch (SqlException e)
  39. {
  40. return HandleConnectionException(e);
  41. }
  42. return new Result(resultCode);
  43. }
  44. #endregion
  45. #region *** Private operations ***
  46. #endregion
  47. }
  48. }