52 lines
1.9 KiB
C#
52 lines
1.9 KiB
C#
using lxc_songlist.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace lxc_songlist.Data;
|
|
|
|
public sealed class SongListDbContext(DbContextOptions<SongListDbContext> options) : DbContext(options)
|
|
{
|
|
public DbSet<Song> Songs => Set<Song>();
|
|
public DbSet<Tag> Tags => Set<Tag>();
|
|
public DbSet<SongTag> SongTags => Set<SongTag>();
|
|
public DbSet<SiteSetting> SiteSettings => Set<SiteSetting>();
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.Entity<Song>(entity =>
|
|
{
|
|
entity.Property(song => song.Title).HasMaxLength(200).IsRequired();
|
|
entity.Property(song => song.NormalizedTitle).HasMaxLength(200).IsRequired();
|
|
entity.HasIndex(song => song.NormalizedTitle).IsUnique();
|
|
});
|
|
|
|
modelBuilder.Entity<Tag>(entity =>
|
|
{
|
|
entity.Property(tag => tag.Name).HasMaxLength(50).IsRequired();
|
|
entity.Property(tag => tag.NormalizedName).HasMaxLength(50).IsRequired();
|
|
entity.HasIndex(tag => tag.NormalizedName).IsUnique();
|
|
});
|
|
|
|
modelBuilder.Entity<SongTag>(entity =>
|
|
{
|
|
entity.HasKey(songTag => new { songTag.SongId, songTag.TagId });
|
|
entity
|
|
.HasOne(songTag => songTag.Song)
|
|
.WithMany(song => song.SongTags)
|
|
.HasForeignKey(songTag => songTag.SongId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
entity
|
|
.HasOne(songTag => songTag.Tag)
|
|
.WithMany(tag => tag.SongTags)
|
|
.HasForeignKey(songTag => songTag.TagId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
});
|
|
|
|
modelBuilder.Entity<SiteSetting>(entity =>
|
|
{
|
|
entity.HasKey(setting => setting.Key);
|
|
entity.Property(setting => setting.Key).HasMaxLength(80);
|
|
entity.Property(setting => setting.Value).HasMaxLength(500);
|
|
});
|
|
}
|
|
}
|