DatabaseService.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using Microsoft.EntityFrameworkCore;
  2. using qdr.app.bundleboiler.business.Services.Interfaces;
  3. using qdr.app.bundleboiler.data.Model;
  4. using Quadarax.Foundation.Core.Attributes;
  5. using Quadarax.Foundation.Core.Data;
  6. using Quadarax.Foundation.Core.Data.Interface.Domain;
  7. using Quadarax.Foundation.Core.Logging;
  8. namespace qdr.app.bundleboiler.business.Services
  9. {
  10. [DiModule]
  11. [DiImplementsOf(typeof(IDatabaseService), LifeCycleTypeEnum.Scoped)]
  12. public class DatabaseService : BaseService, IDatabaseService
  13. {
  14. private readonly IDomain _dbcontext;
  15. public DatabaseService(BoilerDbContext dbcontext, IOptions options, ILogger logger) : base(options, logger)
  16. {
  17. _dbcontext = dbcontext ?? throw new ArgumentNullException(nameof(dbcontext));
  18. }
  19. public IResult EnsureDatabase()
  20. {
  21. return Operation(() =>
  22. {
  23. if (((DbContext)_dbcontext).Database.EnsureCreated())
  24. {
  25. Log.Log(LogSeverityEnum.Info, $"Database @ '{Options.ConnectionString}' created");
  26. }
  27. return Result.Ok;
  28. });
  29. }
  30. public IResult EnsureEnumerations()
  31. {
  32. return Operation(() =>
  33. {
  34. if (((DbContext)_dbcontext).Database.EnsureCreated())
  35. {
  36. Log.Log(LogSeverityEnum.Info, $"Database @ '{Options.ConnectionString}' created");
  37. }
  38. return Result.Ok;
  39. });
  40. }
  41. }
  42. }