Extract BeforeRunHandle method. It runs some procedures like Migrate() on startup.

This commit is contained in:
Фролов 2022-01-17 15:33:00 +05:00
parent 7cf1a69dd5
commit 2192cdc242
4 changed files with 41 additions and 13 deletions

View File

@ -47,14 +47,14 @@ namespace AsbCloudDb.Model
public AsbCloudDbContext() : base() public AsbCloudDbContext() : base()
{ {
Database.Migrate(); //Database.Migrate();
} }
public AsbCloudDbContext(DbContextOptions<AsbCloudDbContext> options) public AsbCloudDbContext(DbContextOptions<AsbCloudDbContext> options)
: base(options) : base(options)
{ {
//Database.SetCommandTimeout(60 * 60 * 2); //Database.SetCommandTimeout(60 * 60 * 2);
Database.Migrate(); //Database.Migrate();
} }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

View File

@ -1,14 +1,12 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace AsbCloudDb.Model namespace AsbCloudDb.Model
{ {
public interface IAsbCloudDbContext public interface IAsbCloudDbContext: IDisposable
{ {
DbSet<Cluster> Clusters { get; set; } DbSet<Cluster> Clusters { get; set; }
DbSet<Company> Companies { get; set; } DbSet<Company> Companies { get; set; }

View File

@ -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<IAsbCloudDbContext>();
context.Database.SetCommandTimeout(TimeSpan.FromSeconds(2 * 60));
context.Database.Migrate();
var wellService = scope.ServiceProvider.GetService<IWellService>();
wellService.EnshureTimezonesIsSet();
}
}
}

View File

@ -1,5 +1,8 @@
using AsbCloudDb.Model;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using System; using System;
using System.Linq; using System.Linq;
@ -10,16 +13,15 @@ namespace AsbCloudWebApi
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
//new TraceListenerView(); // to trace mysterious errors IConfigurationRoot configuration = new ConfigurationBuilder()
if(args?.Length > 0 ) .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json")
.Build();
if (args?.Length > 0 )
{ {
if (args.Contains("db_init")) { if (args.Contains("db_init")) {
var connectionStringName = "DefaultConnection"; var connectionStringName = "DefaultConnection";
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json")
.Build();
AsbCloudInfrastructure.DependencyInjection.MakeContext(configuration.GetConnectionString(connectionStringName)); AsbCloudInfrastructure.DependencyInjection.MakeContext(configuration.GetConnectionString(connectionStringName));
Console.WriteLine("Óñïåøíî âûïîëíåíî."); Console.WriteLine("Óñïåøíî âûïîëíåíî.");
@ -29,7 +31,9 @@ namespace AsbCloudWebApi
return; return;
} }
CreateHostBuilder(args).Build().Run(); var host = CreateHostBuilder(args).Build();
AsbCloudInfrastructure.Startup.BeforeRunHandler(host, configuration);
host.Run();
} }
public static IHostBuilder CreateHostBuilder(string[] args) => public static IHostBuilder CreateHostBuilder(string[] args) =>