76 lines
2.3 KiB
C#
76 lines
2.3 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Shadow.Entities;
|
|
using System.Diagnostics.Metrics;
|
|
using System.Reflection.Emit;
|
|
|
|
namespace Shadow.Data;
|
|
public class ApplicationDbContext : DbContext
|
|
{
|
|
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
|
|
{
|
|
|
|
}
|
|
|
|
// EFC ORM Setup
|
|
public DbSet<Album> Albums => Set<Album>();
|
|
public DbSet<AlbumInteraction> AlbumInteractions => Set<AlbumInteraction>();
|
|
public DbSet<Artist> Artists => Set<Artist>();
|
|
public DbSet<Genre> Genres => Set<Genre>();
|
|
public DbSet<GenreSong> GenreSongs => Set<GenreSong>();
|
|
public DbSet<Image> Images => Set<Image>();
|
|
public DbSet<Playlist> Playlists => Set<Playlist>();
|
|
public DbSet<PlaylistSong> PlaylistSongs => Set<PlaylistSong>();
|
|
public DbSet<PlaylistUser> PlaylistUsers => Set<PlaylistUser>();
|
|
public DbSet<Radio> Radios => Set<Radio>();
|
|
public DbSet<Song> Songs => Set<Song>();
|
|
public DbSet<SongInteraction> SongInteractions => Set<SongInteraction>();
|
|
public DbSet<User> Users => Set<User>();
|
|
|
|
protected override void OnModelCreating(ModelBuilder builder)
|
|
{
|
|
base.OnModelCreating(builder);
|
|
|
|
// Composite keys setup
|
|
builder.Entity<AlbumInteraction>()
|
|
.HasKey(ai => new { ai.AlbumId, ai.UserId });
|
|
builder.Entity<GenreSong>()
|
|
.HasKey(gs => new { gs.GenreId, gs.SongId });
|
|
builder.Entity<PlaylistSong>()
|
|
.HasKey(ps => new { ps.PlaylistId, ps.SongId });
|
|
builder.Entity<PlaylistUser>()
|
|
.HasKey(pu => new { pu.PlaylistId, pu.UserId });
|
|
builder.Entity<SongInteraction>()
|
|
.HasKey(si => new { si.SongId, si.UserId });
|
|
|
|
// Constraints (UQ)
|
|
builder.Entity<Album>(a => {
|
|
a.HasIndex(a => a.Uri).IsUnique();
|
|
});
|
|
builder.Entity<Artist>(a => {
|
|
a.HasIndex(a => a.NormalizedName).IsUnique();
|
|
});
|
|
builder.Entity<Genre>(g => {
|
|
g.HasIndex(g => g.NormalizedName).IsUnique();
|
|
});
|
|
builder.Entity<Playlist>(p => {
|
|
p.HasIndex(p => p.Uri).IsUnique();
|
|
});
|
|
builder.Entity<Radio>(r => {
|
|
r.HasIndex(r => r.NormalizedName).IsUnique();
|
|
});
|
|
builder.Entity<Song>(s => {
|
|
s.HasIndex(s => s.Uri).IsUnique();
|
|
});
|
|
builder.Entity<User>(u => {
|
|
u.HasIndex(u => u.Name).IsUnique();
|
|
// u.HasIndex(u => u.Email).IsUnique();
|
|
});
|
|
|
|
// Force dependent side on Image
|
|
builder.Entity<Image>()
|
|
.HasOne(i => i.Song)
|
|
.WithOne(s => s.Image)
|
|
.HasForeignKey<Image>(i => i.SongId);
|
|
}
|
|
}
|