mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
refactor last using EF Core 2.0 self-contained type configuration
ref #144
This commit is contained in:
parent
bfff929ce5
commit
d5c84ccf1a
8 changed files with 47 additions and 69 deletions
|
|
@ -1,8 +1,5 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using FilterLists.Data.Entities;
|
||||
using FilterLists.Data.EntityMaps;
|
||||
using FilterLists.Data.Entities;
|
||||
using FilterLists.Data.EntityTypeConfigurations;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FilterLists.Data.Contexts
|
||||
|
|
@ -20,19 +17,9 @@ public FilterListsDbContext(DbContextOptions options)
|
|||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
RegisterMaps(modelBuilder);
|
||||
}
|
||||
|
||||
private static void RegisterMaps(ModelBuilder modelBuilder)
|
||||
{
|
||||
var entityMaps = typeof(FilterList).GetTypeInfo().Assembly.GetTypes().Where(type =>
|
||||
!string.IsNullOrWhiteSpace(type.Namespace) && typeof(IEntityMap).IsAssignableFrom(type) &&
|
||||
type.IsClass)
|
||||
.ToList();
|
||||
|
||||
foreach (var entityMap in entityMaps)
|
||||
Activator.CreateInstance(entityMap, BindingFlags.Public | BindingFlags.Instance, null,
|
||||
new object[] {modelBuilder}, null);
|
||||
modelBuilder.ApplyConfiguration(new FilterListConfiguration());
|
||||
modelBuilder.ApplyConfiguration(new MaintainerConfiguration());
|
||||
modelBuilder.ApplyConfiguration(new LanguageConfiguration());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
using FilterLists.Data.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FilterLists.Data.EntityMaps
|
||||
{
|
||||
public class FilterListMap : IEntityMap
|
||||
{
|
||||
public FilterListMap(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<FilterList>()
|
||||
.Property(b => b.CreatedDateUtc)
|
||||
.HasDefaultValueSql("CURRENT_TIMESTAMP");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
namespace FilterLists.Data.EntityMaps
|
||||
{
|
||||
public interface IEntityMap
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
using FilterLists.Data.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FilterLists.Data.EntityMaps
|
||||
{
|
||||
public class LanguageMap : IEntityMap
|
||||
{
|
||||
public LanguageMap(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<Language>()
|
||||
.Property(b => b.CreatedDateUtc)
|
||||
.HasDefaultValueSql("CURRENT_TIMESTAMP");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
using FilterLists.Data.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FilterLists.Data.EntityMaps
|
||||
{
|
||||
public class MaintainerMap : IEntityMap
|
||||
{
|
||||
public MaintainerMap(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<Maintainer>()
|
||||
.Property(b => b.CreatedDateUtc)
|
||||
.HasDefaultValueSql("CURRENT_TIMESTAMP");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
using FilterLists.Data.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace FilterLists.Data.EntityTypeConfigurations
|
||||
{
|
||||
public class FilterListConfiguration : IEntityTypeConfiguration<FilterList>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<FilterList> entityTypeBuilder)
|
||||
{
|
||||
entityTypeBuilder.Property(b => b.CreatedDateUtc).HasDefaultValueSql("CURRENT_TIMESTAMP");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
using FilterLists.Data.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace FilterLists.Data.EntityTypeConfigurations
|
||||
{
|
||||
public class LanguageConfiguration : IEntityTypeConfiguration<Language>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Language> entityTypeBuilder)
|
||||
{
|
||||
entityTypeBuilder.Property(b => b.CreatedDateUtc).HasDefaultValueSql("CURRENT_TIMESTAMP");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
using FilterLists.Data.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace FilterLists.Data.EntityTypeConfigurations
|
||||
{
|
||||
public class MaintainerConfiguration : IEntityTypeConfiguration<Maintainer>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Maintainer> entityTypeBuilder)
|
||||
{
|
||||
entityTypeBuilder.Property(b => b.CreatedDateUtc).HasDefaultValueSql("CURRENT_TIMESTAMP");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue