Files
TeleWave/backend/src/TeleWave.Infrastructure/Persistence/Configurations/GenreConfiguration.cs
T

51 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using TeleWave.Domain.Library;
namespace TeleWave.Infrastructure.Persistence.Configurations;
public class GenreConfiguration : IEntityTypeConfiguration<Genre>
{
public void Configure(EntityTypeBuilder<Genre> builder)
{
builder.Property(x => x.Name).IsRequired().HasMaxLength(128);
builder.Property(x => x.Slug).IsRequired().HasMaxLength(64);
builder.HasIndex(x => x.Slug).IsUnique();
builder
.HasMany(x => x.Aliases)
.WithOne()
.HasForeignKey(a => a.GenreId)
.OnDelete(DeleteBehavior.Cascade);
builder.Navigation(x => x.Aliases).UsePropertyAccessMode(PropertyAccessMode.Field);
}
}
public class GenreAliasConfiguration : IEntityTypeConfiguration<GenreAlias>
{
public void Configure(EntityTypeBuilder<GenreAlias> builder)
{
builder.Property(x => x.Value).IsRequired().HasMaxLength(128);
// Псевдоним однозначно указывает на жанр: иначе сопоставление метаданных стало бы неопределённым.
builder.HasIndex(x => x.Value).IsUnique();
}
}
public class ShowGenreConfiguration : IEntityTypeConfiguration<ShowGenre>
{
public void Configure(EntityTypeBuilder<ShowGenre> builder)
{
builder.HasKey(x => new { x.ShowId, x.GenreId });
builder.HasIndex(x => x.GenreId);
// Restrict, а не Cascade: удаление жанра из справочника не должно молча снимать его со всех шоу.
// Команда удаления проверяет использование заранее и возвращает управляемый конфликт.
builder
.HasOne<Genre>()
.WithMany()
.HasForeignKey(x => x.GenreId)
.OnDelete(DeleteBehavior.Restrict);
}
}