-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(db): use design time context factory
- Loading branch information
Showing
4 changed files
with
42 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/Eurofurence.App.Infrastructure.EntityFramework/AppDbContextFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.IO; | ||
using Eurofurence.App.Infrastructure.EntityFramework; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Design; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
public class AppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext> | ||
{ | ||
public AppDbContext CreateDbContext(string[] args) | ||
{ | ||
// Get environment | ||
string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); | ||
|
||
// Build config | ||
IConfiguration config = new ConfigurationBuilder() | ||
.AddJsonFile("appsettings.json", optional: true) | ||
.AddJsonFile($"appsettings.{environment}.json", optional: true) | ||
.Build(); | ||
|
||
// Get connection string | ||
var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>(); | ||
var connectionString = config.GetConnectionString("Eurofurence"); | ||
|
||
var serverVersionString = Environment.GetEnvironmentVariable("MYSQL_VERSION"); | ||
ServerVersion serverVersion; | ||
if (string.IsNullOrEmpty(serverVersionString) || !ServerVersion.TryParse(serverVersionString, out serverVersion)) | ||
{ | ||
serverVersion = ServerVersion.AutoDetect(connectionString); | ||
} | ||
|
||
optionsBuilder.UseMySql( | ||
connectionString, | ||
serverVersion, | ||
mySqlOptions => mySqlOptions.UseMicrosoftJson()); | ||
return new AppDbContext(optionsBuilder.Options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters