diff --git a/src/FilterLists.Data/Contexts/FilterListsDbContext.cs b/src/FilterLists.Data/Contexts/FilterListsDbContext.cs index fca7cd312..0b9773397 100644 --- a/src/FilterLists.Data/Contexts/FilterListsDbContext.cs +++ b/src/FilterLists.Data/Contexts/FilterListsDbContext.cs @@ -1,4 +1,8 @@ -using FilterLists.Data.Entities; +using System; +using System.Linq; +using System.Reflection; +using FilterLists.Data.Entities; +using FilterLists.Data.EntityMaps; using Microsoft.EntityFrameworkCore; namespace FilterLists.Data.Contexts @@ -16,15 +20,19 @@ public FilterListsDbContext(DbContextOptions options) protected override void OnModelCreating(ModelBuilder modelBuilder) { - modelBuilder.Entity() - .Property(b => b.CreatedDateUtc) - .HasDefaultValueSql("CURRENT_TIMESTAMP"); - modelBuilder.Entity() - .Property(b => b.CreatedDateUtc) - .HasDefaultValueSql("CURRENT_TIMESTAMP"); - modelBuilder.Entity() - .Property(b => b.CreatedDateUtc) - .HasDefaultValueSql("CURRENT_TIMESTAMP"); + 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); } } } \ No newline at end of file diff --git a/src/FilterLists.Data/EntityMaps/FilterListMap.cs b/src/FilterLists.Data/EntityMaps/FilterListMap.cs new file mode 100644 index 000000000..a00d415b8 --- /dev/null +++ b/src/FilterLists.Data/EntityMaps/FilterListMap.cs @@ -0,0 +1,15 @@ +using FilterLists.Data.Entities; +using Microsoft.EntityFrameworkCore; + +namespace FilterLists.Data.EntityMaps +{ + public class FilterListMap : IEntityMap + { + public FilterListMap(ModelBuilder modelBuilder) + { + modelBuilder.Entity() + .Property(b => b.CreatedDateUtc) + .HasDefaultValueSql("CURRENT_TIMESTAMP"); + } + } +} \ No newline at end of file diff --git a/src/FilterLists.Data/EntityMaps/IEntityMap.cs b/src/FilterLists.Data/EntityMaps/IEntityMap.cs new file mode 100644 index 000000000..0d5af4baa --- /dev/null +++ b/src/FilterLists.Data/EntityMaps/IEntityMap.cs @@ -0,0 +1,6 @@ +namespace FilterLists.Data.EntityMaps +{ + public interface IEntityMap + { + } +} \ No newline at end of file diff --git a/src/FilterLists.Data/EntityMaps/LanguageMap.cs b/src/FilterLists.Data/EntityMaps/LanguageMap.cs new file mode 100644 index 000000000..6720d69cc --- /dev/null +++ b/src/FilterLists.Data/EntityMaps/LanguageMap.cs @@ -0,0 +1,15 @@ +using FilterLists.Data.Entities; +using Microsoft.EntityFrameworkCore; + +namespace FilterLists.Data.EntityMaps +{ + public class LanguageMap : IEntityMap + { + public LanguageMap(ModelBuilder modelBuilder) + { + modelBuilder.Entity() + .Property(b => b.CreatedDateUtc) + .HasDefaultValueSql("CURRENT_TIMESTAMP"); + } + } +} \ No newline at end of file diff --git a/src/FilterLists.Data/EntityMaps/MaintainerMap.cs b/src/FilterLists.Data/EntityMaps/MaintainerMap.cs new file mode 100644 index 000000000..4267a26f7 --- /dev/null +++ b/src/FilterLists.Data/EntityMaps/MaintainerMap.cs @@ -0,0 +1,15 @@ +using FilterLists.Data.Entities; +using Microsoft.EntityFrameworkCore; + +namespace FilterLists.Data.EntityMaps +{ + public class MaintainerMap : IEntityMap + { + public MaintainerMap(ModelBuilder modelBuilder) + { + modelBuilder.Entity() + .Property(b => b.CreatedDateUtc) + .HasDefaultValueSql("CURRENT_TIMESTAMP"); + } + } +} \ No newline at end of file