diff --git a/AsbCloudDb/Model/AsbCloudDbContext.cs b/AsbCloudDb/Model/AsbCloudDbContext.cs index 188e875d..22f94ac4 100644 --- a/AsbCloudDb/Model/AsbCloudDbContext.cs +++ b/AsbCloudDb/Model/AsbCloudDbContext.cs @@ -47,14 +47,14 @@ namespace AsbCloudDb.Model public AsbCloudDbContext() : base() { - Database.Migrate(); + //Database.Migrate(); } public AsbCloudDbContext(DbContextOptions options) : base(options) { //Database.SetCommandTimeout(60 * 60 * 2); - Database.Migrate(); + //Database.Migrate(); } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) diff --git a/AsbCloudDb/Model/IAsbCloudDbContext.cs b/AsbCloudDb/Model/IAsbCloudDbContext.cs index 32c70df7..dc3e5473 100644 --- a/AsbCloudDb/Model/IAsbCloudDbContext.cs +++ b/AsbCloudDb/Model/IAsbCloudDbContext.cs @@ -1,14 +1,12 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using System; -using System.Collections.Generic; -using System.Linq; using System.Threading; using System.Threading.Tasks; namespace AsbCloudDb.Model { - public interface IAsbCloudDbContext + public interface IAsbCloudDbContext: IDisposable { DbSet Clusters { get; set; } DbSet Companies { get; set; } diff --git a/AsbCloudInfrastructure/Startup.cs b/AsbCloudInfrastructure/Startup.cs new file mode 100644 index 00000000..67f7e14f --- /dev/null +++ b/AsbCloudInfrastructure/Startup.cs @@ -0,0 +1,26 @@ +using AsbCloudApp.Services; +using AsbCloudDb.Model; +using AsbCloudInfrastructure.Services.Cache; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using System; + +namespace AsbCloudInfrastructure +{ + public class Startup + { + public static void BeforeRunHandler(IHost host, IConfigurationRoot configuration) + { + using var scope = host.Services.CreateScope(); + var context = scope.ServiceProvider.GetService(); + context.Database.SetCommandTimeout(TimeSpan.FromSeconds(2 * 60)); + + context.Database.Migrate(); + + var wellService = scope.ServiceProvider.GetService(); + wellService.EnshureTimezonesIsSet(); + } + } +} diff --git a/AsbCloudWebApi/Program.cs b/AsbCloudWebApi/Program.cs index 395ec1e7..e33ea74b 100644 --- a/AsbCloudWebApi/Program.cs +++ b/AsbCloudWebApi/Program.cs @@ -1,5 +1,8 @@ +using AsbCloudDb.Model; using Microsoft.AspNetCore.Hosting; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System; using System.Linq; @@ -10,16 +13,15 @@ namespace AsbCloudWebApi { public static void Main(string[] args) { - //new TraceListenerView(); // to trace mysterious errors - if(args?.Length > 0 ) + IConfigurationRoot configuration = new ConfigurationBuilder() + .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) + .AddJsonFile("appsettings.json") + .Build(); + + if (args?.Length > 0 ) { if (args.Contains("db_init")) { var connectionStringName = "DefaultConnection"; - - IConfigurationRoot configuration = new ConfigurationBuilder() - .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) - .AddJsonFile("appsettings.json") - .Build(); AsbCloudInfrastructure.DependencyInjection.MakeContext(configuration.GetConnectionString(connectionStringName)); Console.WriteLine("Óñïåøíî âûïîëíåíî."); @@ -29,7 +31,9 @@ namespace AsbCloudWebApi return; } - CreateHostBuilder(args).Build().Run(); + var host = CreateHostBuilder(args).Build(); + AsbCloudInfrastructure.Startup.BeforeRunHandler(host, configuration); + host.Run(); } public static IHostBuilder CreateHostBuilder(string[] args) =>