Refactored to use postgres for production and sqlite for development

This commit is contained in:
Christopher Sanden
2026-03-27 19:13:17 +01:00
committed by Chris
parent b837e1aad2
commit 416d668294
17 changed files with 2218 additions and 38 deletions

View File

@@ -1,30 +1,24 @@
using Example.Models;
using Example.Models;
namespace Example.Data
namespace Example.Data;
public static class ApplicationDbInitializer
{
public static class ApplicationDbInitializer
public static void SeedDevelopmentData(ApplicationDbContext db)
{
public static void Initialize(ApplicationDbContext db)
if (db.Authors.Any())
{
// Delete the database before we initialize it. This is common to do during development.
db.Database.EnsureDeleted();
// Recreate the database and tables according to our models
db.Database.EnsureCreated();
// Add test data to simplify debugging and testing
var authors = new[]
{
new Author("Author 1", "Author 1", new DateTime(1981, 1, 1)),
new Author("Author 2", "Author 2", new DateTime(1982, 2, 2)),
new Author("Author 3", "Author 3", new DateTime(1983, 3, 3))
};
db.Authors.AddRange(authors);
// Finally save the added relationships
db.SaveChanges();
return;
}
var authors = new[]
{
new Author("Author 1", "Author 1", new DateTime(1981, 1, 1)),
new Author("Author 2", "Author 2", new DateTime(1982, 2, 2)),
new Author("Author 3", "Author 3", new DateTime(1983, 3, 3))
};
db.Authors.AddRange(authors);
db.SaveChanges();
}
}