forked from ddrilling/AsbCloudServer
Extract BeforeRunHandle method. It runs some procedures like Migrate() on startup.
This commit is contained in:
parent
7cf1a69dd5
commit
2192cdc242
@ -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)
|
||||||
|
@ -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; }
|
||||||
|
26
AsbCloudInfrastructure/Startup.cs
Normal file
26
AsbCloudInfrastructure/Startup.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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,17 +13,16 @@ namespace AsbCloudWebApi
|
|||||||
{
|
{
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
//new TraceListenerView(); // to trace mysterious errors
|
|
||||||
if(args?.Length > 0 )
|
|
||||||
{
|
|
||||||
if (args.Contains("db_init")) {
|
|
||||||
var connectionStringName = "DefaultConnection";
|
|
||||||
|
|
||||||
IConfigurationRoot configuration = new ConfigurationBuilder()
|
IConfigurationRoot configuration = new ConfigurationBuilder()
|
||||||
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
|
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
|
||||||
.AddJsonFile("appsettings.json")
|
.AddJsonFile("appsettings.json")
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
|
if (args?.Length > 0 )
|
||||||
|
{
|
||||||
|
if (args.Contains("db_init")) {
|
||||||
|
var connectionStringName = "DefaultConnection";
|
||||||
|
|
||||||
AsbCloudInfrastructure.DependencyInjection.MakeContext(configuration.GetConnectionString(connectionStringName));
|
AsbCloudInfrastructure.DependencyInjection.MakeContext(configuration.GetConnectionString(connectionStringName));
|
||||||
Console.WriteLine("Óñïåøíî âûïîëíåíî.");
|
Console.WriteLine("Óñïåøíî âûïîëíåíî.");
|
||||||
return;
|
return;
|
||||||
@ -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) =>
|
||||||
|
Loading…
Reference in New Issue
Block a user