DbContextCommand.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using Microsoft.Data.SqlClient;
  2. using Quadarax.Application.QLiberace.Base;
  3. using Quadarax.Foundation.Core.QConsole.Command.Base;
  4. using Quadarax.Foundation.Core.QConsole;
  5. using Quadarax.Foundation.Core.Value;
  6. using Microsoft.EntityFrameworkCore;
  7. namespace Quadarax.Application.QLiberace.Console.Commands.Base
  8. {
  9. internal abstract class DbContextCommand : AbstractCommand
  10. {
  11. #region *** Properties ***
  12. #endregion
  13. #region *** Constructor ***
  14. public DbContextCommand(Engine engine) : base(engine)
  15. {
  16. }
  17. #endregion
  18. #region *** EXECUTE ***
  19. protected override Result OnExecute()
  20. {
  21. var options = new DbContextOptions<QlbrcDbContext>();
  22. using (var context = new QlbrcDbContext(options))
  23. {
  24. return OnExecute(context);
  25. }
  26. }
  27. protected abstract Result OnExecute(QlbrcDbContext context);
  28. #endregion
  29. #region *** Private operations ***
  30. protected Result HandleConnectionException(SqlException e)
  31. {
  32. if (e.Message.Contains("A network-related or instance-specific error occurred while establishing a connection to SQL Server"))
  33. {
  34. WriteError(e);
  35. return new Result(Constants.ReturnCodes.CannotConnectToServer);
  36. }
  37. throw e;
  38. }
  39. #endregion
  40. }
  41. }