Skip to content

Commit 2d37507

Browse files
committed
Making the code startup in first run without a connectionstring.
1 parent 146729e commit 2d37507

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

eFormAPI/eFormAPI.Web/Program.cs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,36 @@ public static void MigrateDb(IWebHost webHost)
5454
{
5555
using (var scope = webHost.Services.GetService<IServiceScopeFactory>().CreateScope())
5656
{
57-
using (var dbContext = scope.ServiceProvider.GetRequiredService<BaseDbContext>())
57+
58+
BaseDbContext dbContext = null;
59+
try
60+
{
61+
dbContext = scope.ServiceProvider.GetRequiredService<BaseDbContext>();
62+
}
63+
catch {}
64+
65+
if (dbContext != null)
5866
{
59-
try
67+
using (dbContext = scope.ServiceProvider.GetRequiredService<BaseDbContext>())
6068
{
61-
var connectionStrings =
62-
scope.ServiceProvider.GetRequiredService<IWritableOptions<ConnectionStrings>>();
63-
if (connectionStrings.Value.DefaultConnection != "...")
69+
try
6470
{
65-
dbContext.Database.Migrate();
71+
var connectionStrings =
72+
scope.ServiceProvider.GetRequiredService<IWritableOptions<ConnectionStrings>>();
73+
if (connectionStrings.Value.DefaultConnection != "...")
74+
{
75+
dbContext.Database.Migrate();
76+
}
6677
}
67-
}
68-
catch (Exception e)
69-
{
70-
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
71-
logger.LogError(e, "Error while migrating db");
72-
}
78+
catch (Exception e)
79+
{
80+
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
81+
logger.LogError(e, "Error while migrating db");
82+
}
83+
}
7384
}
85+
86+
7487
}
7588
}
7689

0 commit comments

Comments
 (0)