mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
feat(directory): ✨ configure many-to-many relationships
This commit is contained in:
parent
6ea2df9636
commit
5216d9577d
5 changed files with 122 additions and 1 deletions
|
|
@ -1,4 +1,6 @@
|
|||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Entities;
|
||||
using System;
|
||||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Entities;
|
||||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Mappings;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FilterLists.Directory.Infrastructure.Persistence.Queries.Facade
|
||||
|
|
@ -16,5 +18,18 @@ public DirectoryQueryDbContext(DbContextOptions<DirectoryQueryDbContext> options
|
|||
public DbSet<Software> Software => Set<Software>();
|
||||
public DbSet<Syntax> Syntaxes => Set<Syntax>();
|
||||
public DbSet<Tag> Tags => Set<Tag>();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
_ = modelBuilder ?? throw new ArgumentNullException(nameof(modelBuilder));
|
||||
|
||||
//TODO: figure out why this extension doesn't load configs
|
||||
modelBuilder.ApplyConfigurationsFromAssembly(GetType().Assembly);
|
||||
|
||||
modelBuilder.ApplyConfiguration(new DependentTypeConfiguration<Dependent>());
|
||||
modelBuilder.ApplyConfiguration(new FilterListLanguageTypeConfiguration<FilterListLanguage>());
|
||||
modelBuilder.ApplyConfiguration(new ForkTypeConfiguration<Fork>());
|
||||
modelBuilder.ApplyConfiguration(new MergeTypeConfiguration<Merge>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace FilterLists.Directory.Infrastructure.Persistence.Queries.Mappings
|
||||
{
|
||||
internal class DependentTypeConfiguration<TEntity> : IEntityTypeConfiguration<TEntity> where TEntity : Dependent
|
||||
{
|
||||
public virtual void Configure(EntityTypeBuilder<TEntity> builder)
|
||||
{
|
||||
_ = builder ?? throw new ArgumentNullException(nameof(builder));
|
||||
|
||||
const string dependencyFilterListId = nameof(Dependent.DependencyFilterList) + "Id";
|
||||
const string dependentFilterListId = nameof(Dependent.DependentFilterList) + "Id";
|
||||
builder.Property<ushort>(dependencyFilterListId);
|
||||
builder.Property<ushort>(dependentFilterListId);
|
||||
builder.HasKey(dependencyFilterListId, dependentFilterListId);
|
||||
builder.HasOne(d => d.DependencyFilterList)
|
||||
.WithMany(f => (IEnumerable<TEntity>)f.DependencyFilterLists)
|
||||
.HasForeignKey(dependencyFilterListId);
|
||||
builder.HasOne(d => d.DependentFilterList)
|
||||
.WithMany(f => (IEnumerable<TEntity>)f.DependentFilterLists)
|
||||
.HasForeignKey(dependentFilterListId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
using System;
|
||||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace FilterLists.Directory.Infrastructure.Persistence.Queries.Mappings
|
||||
{
|
||||
internal class FilterListLanguageTypeConfiguration<TEntity> : IEntityTypeConfiguration<TEntity>
|
||||
where TEntity : FilterListLanguage
|
||||
{
|
||||
public virtual void Configure(EntityTypeBuilder<TEntity> builder)
|
||||
{
|
||||
_ = builder ?? throw new ArgumentNullException(nameof(builder));
|
||||
|
||||
const string filterListId = nameof(FilterListLanguage.FilterList) + "Id";
|
||||
const string languageId = nameof(FilterListLanguage.Language) + "Id";
|
||||
builder.Property<ushort>(filterListId);
|
||||
builder.Property<ushort>(languageId);
|
||||
builder.HasKey(filterListId, languageId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace FilterLists.Directory.Infrastructure.Persistence.Queries.Mappings
|
||||
{
|
||||
internal class ForkTypeConfiguration<TEntity> : IEntityTypeConfiguration<TEntity> where TEntity : Fork
|
||||
{
|
||||
public virtual void Configure(EntityTypeBuilder<TEntity> builder)
|
||||
{
|
||||
_ = builder ?? throw new ArgumentNullException(nameof(builder));
|
||||
|
||||
const string upstreamFilterListId = nameof(Fork.UpstreamFilterList) + "Id";
|
||||
const string forkFilterListId = nameof(Fork.ForkFilterList) + "Id";
|
||||
builder.Property<ushort>(upstreamFilterListId);
|
||||
builder.Property<ushort>(forkFilterListId);
|
||||
builder.HasKey(upstreamFilterListId, forkFilterListId);
|
||||
builder.HasOne(f => f.UpstreamFilterList)
|
||||
.WithMany(f => (IEnumerable<TEntity>)f.UpstreamFilterLists)
|
||||
.HasForeignKey(upstreamFilterListId);
|
||||
builder.HasOne(f => f.ForkFilterList)
|
||||
.WithMany(f => (IEnumerable<TEntity>)f.ForkFilterLists)
|
||||
.HasForeignKey(forkFilterListId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace FilterLists.Directory.Infrastructure.Persistence.Queries.Mappings
|
||||
{
|
||||
internal class MergeTypeConfiguration<TEntity> : IEntityTypeConfiguration<TEntity> where TEntity : Merge
|
||||
{
|
||||
public virtual void Configure(EntityTypeBuilder<TEntity> builder)
|
||||
{
|
||||
_ = builder ?? throw new ArgumentNullException(nameof(builder));
|
||||
|
||||
const string includedInFilterListId = nameof(Merge.IncludedInFilterList) + "Id";
|
||||
const string includesFilterListId = nameof(Merge.IncludesFilterList) + "Id";
|
||||
builder.Property<ushort>(includedInFilterListId);
|
||||
builder.Property<ushort>(includesFilterListId);
|
||||
builder.HasKey(includedInFilterListId, includesFilterListId);
|
||||
builder.HasOne(m => m.IncludedInFilterList)
|
||||
.WithMany(f => (IEnumerable<TEntity>)f.IncludedInFilterLists)
|
||||
.HasForeignKey(includedInFilterListId);
|
||||
builder.HasOne(m => m.IncludesFilterList)
|
||||
.WithMany(f => (IEnumerable<TEntity>)f.IncludesFilterLists)
|
||||
.HasForeignKey(includesFilterListId);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue