Skip to content

Commit 4c56154

Browse files
authored
Merge pull request #171 from VolvoxCommunity/refactor-context-factory
Refactor context factory
2 parents 3431ff0 + 89bc3be commit 4c56154

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.IO;
3+
using Microsoft.Extensions.Configuration;
4+
5+
namespace Volvox.Helios.Web
6+
{
7+
public static class ConfigurationHelper
8+
{
9+
public static IConfiguration GetDefaultConfiguration()
10+
{
11+
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
12+
return new ConfigurationBuilder()
13+
.SetBasePath(Directory.GetCurrentDirectory())
14+
.AddJsonFile("./appsettings.json", optional: false, reloadOnChange: true)
15+
.AddJsonFile($"./appsettings.{environment}.json", optional: true, reloadOnChange: true)
16+
.AddJsonFile("./modulemetadata.json")
17+
.AddUserSecrets<Startup>().Build();
18+
}
19+
}
20+
}

src/Volvox.Helios.Web/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static IWebHostBuilder CreateWebHostBuilder(string[] args)
1717
return WebHost.CreateDefaultBuilder(args)
1818
.UseApplicationInsights()
1919
.UseStartup<Startup>()
20-
.ConfigureAppConfiguration(c => c.AddJsonFile("./modulemetadata.json"))
20+
.UseConfiguration(ConfigurationHelper.GetDefaultConfiguration())
2121
.ConfigureLogging((hostingContext, logging) =>
2222
{
2323
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));

src/Volvox.Helios.Service/VolvoxHeliosContextFactory.cs src/Volvox.Helios.Web/VolvoxHeliosContextFactory.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
using Microsoft.EntityFrameworkCore;
22
using Microsoft.EntityFrameworkCore.Design;
3+
using Microsoft.Extensions.Configuration;
4+
using Volvox.Helios.Service;
35

4-
namespace Volvox.Helios.Service
6+
namespace Volvox.Helios.Web
57
{
68
public class VolvoxHeliosContextFactory : IDesignTimeDbContextFactory<VolvoxHeliosContext>
79
{
810
public VolvoxHeliosContext CreateDbContext(string[] args)
911
{
12+
var configuration = ConfigurationHelper.GetDefaultConfiguration();
13+
1014
var optionsBuilder = new DbContextOptionsBuilder<VolvoxHeliosContext>();
11-
optionsBuilder.UseSqlServer(
12-
"Server=(localdb)\\mssqllocaldb;Database=VolvoxHelios;Trusted_Connection=True;");
1315

16+
optionsBuilder.UseSqlServer(configuration.GetConnectionString("VolvoxHeliosDatabase"), options=>
17+
options.MigrationsAssembly("Volvox.Helios.Service"));
1418
return new VolvoxHeliosContext(optionsBuilder.Options);
1519
}
1620
}

0 commit comments

Comments
 (0)