API Test
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
using API.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Model;
|
||||
|
||||
namespace API.Data;
|
||||
|
||||
public class AppDbContext : DbContext
|
||||
{
|
||||
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
public DbSet<User> Users { get; set; } = default!;
|
||||
public DbSet<CardNote> CardNotes { get; set; } = default!;
|
||||
public DbSet<UserDeck> UserDecks { get; set; } = default!;
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<User>(b => { b.HasKey(u => u.Username); });
|
||||
|
||||
modelBuilder.Entity<CardNote>(b => { b.HasKey(n => new { n.Username, n.CardName }); });
|
||||
|
||||
modelBuilder.Entity<UserDeck>(b =>
|
||||
{
|
||||
b.HasKey(d => d.Id);
|
||||
if (Database.IsNpgsql())
|
||||
{
|
||||
b.Property(d => d.Cards).HasColumnType("jsonb");
|
||||
b.Property(d => d.Divers).HasColumnType("jsonb");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
using Npgsql;
|
||||
|
||||
namespace API.Data;
|
||||
|
||||
public class AppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
|
||||
{
|
||||
public AppDbContext CreateDbContext(string[] args)
|
||||
{
|
||||
var envPassword = Environment.GetEnvironmentVariable("PostgreSQL_Password")
|
||||
?? (OperatingSystem.IsWindows()
|
||||
? Environment.GetEnvironmentVariable("PostgreSQL_Password",
|
||||
EnvironmentVariableTarget.User)
|
||||
: null)
|
||||
?? (OperatingSystem.IsWindows()
|
||||
? Environment.GetEnvironmentVariable("PostgreSQL_Password",
|
||||
EnvironmentVariableTarget.Machine)
|
||||
: null)
|
||||
?? Environment.GetEnvironmentVariable("POSTGRESQL_PASSWORD")
|
||||
?? Environment.GetEnvironmentVariable("POSTGRES_PASSWORD");
|
||||
|
||||
var csb = new NpgsqlConnectionStringBuilder
|
||||
{
|
||||
Host = "localhost",
|
||||
Database = "chrono",
|
||||
Username = "postgres",
|
||||
Password = envPassword
|
||||
};
|
||||
|
||||
var options = new DbContextOptionsBuilder<AppDbContext>()
|
||||
.UseNpgsql(csb.ConnectionString)
|
||||
.Options;
|
||||
return new AppDbContext(options);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user