mirror of
https://github.com/rmcrackan/Libation.git
synced 2025-12-23 22:17:52 -05:00
Supporting postgres simplifies deployments to environments such as kubernetes. Since sqlite doesn't work well on nfs shares it can be easier for databases to have a dedicated db set up that applications can connect to. Sqlite is easier for most deployments though, so this will default to that if the settings haven't been updated to support it. This change does the following: - Separate out SQLite from the DataLayer and adds a Postgres assembly for migrations as well - Add a configuration setting for a postgres connection string that will be used if it is there, otherwise reverts to the original sqlite string - Add a copydb command for the cli to bootstrap the postgres db - A convenience script to update migrations for both dbs at the same time
65 lines
3.0 KiB
C#
65 lines
3.0 KiB
C#
using DataLayer.Configurations;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DataLayer
|
|
{
|
|
public class LibationContext : DbContext, INotifyDisposed
|
|
{
|
|
// IMPORTANT: USING DbSet<>
|
|
// ========================
|
|
// these run against the db. linq queries against these MUST be translatable to sql. primatives only. no POCOs or this error occurs:
|
|
// Unable to create a constant value of type 'DataLayer.Contributor'. Only primitive types or enumeration types are supported in this context.
|
|
// to use full object-linq, load and use Local. HOWEVER, Local is only hashed/indexed on PK. All other searches are very slow
|
|
// load full table:
|
|
// List<Contributor> contributors = ...;
|
|
// Contributors.Load();
|
|
// Contributors.Local.Where(a => contributors.Contains(a));
|
|
// load only those in object:
|
|
// // overwrite collection
|
|
// Entry(product).Collection(x => x.Narrators).Load();
|
|
// product.Narrators = narrators;
|
|
public DbSet<LibraryBook> LibraryBooks { get; private set; }
|
|
public DbSet<Book> Books { get; private set; }
|
|
public DbSet<Contributor> Contributors { get; private set; }
|
|
public DbSet<Series> Series { get; private set; }
|
|
public DbSet<Category> Categories { get; private set; }
|
|
public DbSet<CategoryLadder> CategoryLadders { get; private set; }
|
|
|
|
public event EventHandler ObjectDisposed;
|
|
public override void Dispose()
|
|
{
|
|
base.Dispose();
|
|
ObjectDisposed?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
public override async ValueTask DisposeAsync()
|
|
{
|
|
await base.DisposeAsync();
|
|
ObjectDisposed?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
|
|
// see DesignTimeDbContextFactoryBase for info about ctors and connection strings/OnConfiguring()
|
|
public LibationContext(DbContextOptions options) : base(options) { }
|
|
|
|
// typically only called once per execution; NOT once per instantiation
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
modelBuilder.ApplyConfiguration(new BookConfig());
|
|
modelBuilder.ApplyConfiguration(new ContributorConfig());
|
|
modelBuilder.ApplyConfiguration(new BookContributorConfig());
|
|
modelBuilder.ApplyConfiguration(new LibraryBookConfig());
|
|
modelBuilder.ApplyConfiguration(new SeriesConfig());
|
|
modelBuilder.ApplyConfiguration(new SeriesBookConfig());
|
|
modelBuilder.ApplyConfiguration(new CategoryConfig());
|
|
modelBuilder.ApplyConfiguration(new CategoryLadderConfig());
|
|
modelBuilder.ApplyConfiguration(new BookCategoryConfig());
|
|
|
|
// views are now supported via "keyless entity types" (instead of "entity types" or the prev "query types"):
|
|
// https://docs.microsoft.com/en-us/ef/core/modeling/keyless-entity-types
|
|
}
|
|
}
|
|
}
|