From d8d42d0487b99fb576bdcd0f0323f6ede4a0b132 Mon Sep 17 00:00:00 2001 From: Daniel Thorne Date: Wed, 20 Mar 2024 18:24:51 -0600 Subject: [PATCH 1/2] Updated EF Core to v6.0.28 --- DJT.EntityFrameworkCore/DJT.EntityFrameworkCore.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DJT.EntityFrameworkCore/DJT.EntityFrameworkCore.csproj b/DJT.EntityFrameworkCore/DJT.EntityFrameworkCore.csproj index a401983..a1208b7 100644 --- a/DJT.EntityFrameworkCore/DJT.EntityFrameworkCore.csproj +++ b/DJT.EntityFrameworkCore/DJT.EntityFrameworkCore.csproj @@ -16,7 +16,7 @@ - + From 6fedabec38de3c3c235635b6dc76b7c90e65a366 Mon Sep 17 00:00:00 2001 From: Daniel Thorne Date: Wed, 20 Mar 2024 18:32:22 -0600 Subject: [PATCH 2/2] Breaking: `UseSelfDefiningModels` to cover all entities in the model, not just from `DbSet`s. --- .../DbContextExtensions.cs | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/DJT.EntityFrameworkCore/DbContextExtensions.cs b/DJT.EntityFrameworkCore/DbContextExtensions.cs index 95ac59a..3d422b8 100644 --- a/DJT.EntityFrameworkCore/DbContextExtensions.cs +++ b/DJT.EntityFrameworkCore/DbContextExtensions.cs @@ -21,24 +21,23 @@ public static class DbContextExtensions /// If entities are internal, the assembly attribute may /// need specifying. /// - /// /// - public static void UseSelfDefiningModels(this DbContext dbContext, ModelBuilder modelBuilder) + public static void UseSelfDefiningModels(this ModelBuilder modelBuilder) { - Type dbContextType = dbContext.GetType(); - foreach (var prop in dbContextType.GetProperties()) + var entities = modelBuilder.Model.GetEntityTypes(); + foreach (var entity in entities) { - var propType = prop.PropertyType; - if (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(DbSet<>)) + modelBuilder.Entity(entity.Name, builder => { - var itemType = propType.GenericTypeArguments[0];//.GetGenericTypeDefinition().GetGenericArguments()[0]; - var method = itemType.GetMethod("OnModelCreating"); - if (typeof(ISelfDefine).IsAssignableFrom(itemType) && method != null) + Type itemType = entity.ClrType; + + var method = itemType.GetMethod(nameof(ISelfDefine.OnModelCreating)); + if (typeof(ISelfDefine).IsAssignableFrom(itemType) && method is not null) { - //If the "OnModelCreating(ModelBuilder modelBuilder)" method exists, run it with the given ModelBuilder - method.Invoke(Activator.CreateInstance(itemType, false), new object[] { modelBuilder }); + method.Invoke(Activator.CreateInstance(itemType, false), + new object[] { modelBuilder }); } - } + }); } } }