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()
{
Database.Migrate();
//Database.Migrate();
}
public AsbCloudDbContext(DbContextOptions<AsbCloudDbContext> options)
: base(options)
{
//Database.SetCommandTimeout(60 * 60 * 2);
Database.Migrate();
//Database.Migrate();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

View File

@ -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<Cluster> Clusters { 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.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) =>