Fix db seeding, migration, repository services
This commit is contained in:
parent
872dc1e263
commit
6b87902ca7
22 changed files with 606 additions and 64 deletions
36
CSR.Infrastructure/Persistence/CSRDbContextFactory.cs
Normal file
36
CSR.Infrastructure/Persistence/CSRDbContextFactory.cs
Normal file
|
@ -0,0 +1,36 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace CSR.Infrastructure.Persistence
|
||||
{
|
||||
public class CSRDbContextFactory : IDesignTimeDbContextFactory<CSRDbContext>
|
||||
{
|
||||
public CSRDbContext CreateDbContext(string[] args)
|
||||
{
|
||||
// build configuration.
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "..", "CSR.WebUI"))
|
||||
.AddJsonFile("appsettings.json", optional: true)
|
||||
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development"}.json", optional: true)
|
||||
.AddEnvironmentVariables()
|
||||
.AddKeyPerFile("/run/secrets", optional: true)
|
||||
.Build();
|
||||
|
||||
// get the database path
|
||||
var dbPath = configuration["Database:Path"];
|
||||
if (string.IsNullOrEmpty(dbPath))
|
||||
{
|
||||
var folder = Environment.SpecialFolder.LocalApplicationData;
|
||||
var path = Environment.GetFolderPath(folder);
|
||||
dbPath = Path.Join(path, "csr.db");
|
||||
}
|
||||
|
||||
// create DbContextOptions
|
||||
var optionsBuilder = new DbContextOptionsBuilder<CSRDbContext>();
|
||||
optionsBuilder.UseSqlite($"Data Source={dbPath}");
|
||||
|
||||
return new CSRDbContext(optionsBuilder.Options);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue