Add startup argument db_init to start just for migrate DB

This commit is contained in:
Фролов 2021-12-02 11:11:14 +05:00
parent c55086bfee
commit fdf5a6ecd6
2 changed files with 42 additions and 0 deletions

View File

@ -14,6 +14,15 @@ namespace AsbCloudInfrastructure
{
public static class DependencyInjection
{
public static IAsbCloudDbContext MakeContext(string connectionString)
{
var options = new DbContextOptionsBuilder<AsbCloudDbContext>()
.UseNpgsql(connectionString)
.Options;
var context = new AsbCloudDbContext(options);
return context;
}
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
{
services.AddDbContext<AsbCloudDbContext>(options =>
@ -79,5 +88,6 @@ namespace AsbCloudInfrastructure
where TImplementation : class, TService
=> services.AddTransient<TService, TImplementation>(implementationFactory)
.AddTransient(provider => new Lazy<TService>(() => implementationFactory(provider)));
}
}

View File

@ -1,5 +1,8 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System;
using System.Linq;
namespace AsbCloudWebApi
{
@ -8,6 +11,23 @@ namespace AsbCloudWebApi
public static void Main(string[] args)
{
//new TraceListenerView(); // to trace mysterious errors
if(args?.Length > 1 )
{
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("Óñïåøíî âûïîëíåíî.");
return;
}
WriteHelp();
}
CreateHostBuilder(args).Build().Run();
}
@ -17,5 +37,17 @@ namespace AsbCloudWebApi
{
webBuilder.UseStartup<Startup>();
});
private static void WriteHelp()
{
Console.WriteLine("Ïðè çàïóñêå áåç êëþ÷åé ïðîãðàììà ïðîñòî ñòàðòóåò â îáû÷íîì ðåæèìå.");
Console.WriteLine("Êëþ÷è äëÿ çàïóñêà:");
Console.WriteLine("db_init - ñîçäàòü êîíòåêñò ÁÄ è âûéòè.");
Console.WriteLine("Êîíòåêñò ñîçäàñòñÿ äëÿ ñòðîêè ïîäêëþ÷åíèÿ \"DefaultConnection\"");
Console.WriteLine("Ñîçäàíèå êîíòåêñòà ïðèâåäåò ê ñîçäàíèþ ÁÄ, åñëè åé íåò");
Console.WriteLine("è ïðèìåíåíèþ âñåõ ìèãðàöèé, åñëè ÁÄ óæå åñòü.");
Console.WriteLine("Äëÿ ñîçäàíèÿ êîíòåêñòà â ÁÄ äîëæíà áûòü ñîçäàíà ñõåìà public");
Console.WriteLine("");
}
}
}