2024-05-30 20:19:58 +03:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace Mirea.Api.DataAccess.Persistence.Common;
|
|
|
|
|
|
|
|
|
|
public static class ModelBuilderExtensions
|
|
|
|
|
{
|
|
|
|
|
public static void ApplyConfiguration(this ModelBuilder modelBuilder, object configuration)
|
|
|
|
|
{
|
|
|
|
|
var applyGenericMethod = typeof(ModelBuilder)
|
|
|
|
|
.GetMethods()
|
2024-12-25 05:43:30 +03:00
|
|
|
|
.First(m => m.Name == nameof(ApplyConfiguration) &&
|
2024-05-30 20:19:58 +03:00
|
|
|
|
m.GetParameters().Any(p => p.ParameterType.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration<>)));
|
|
|
|
|
|
|
|
|
|
var entityType = configuration.GetType().GetInterfaces()
|
|
|
|
|
.First(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration<>))
|
|
|
|
|
.GetGenericArguments()[0];
|
|
|
|
|
|
|
|
|
|
var applyConcreteMethod = applyGenericMethod.MakeGenericMethod(entityType);
|
|
|
|
|
applyConcreteMethod.Invoke(modelBuilder, [configuration]);
|
|
|
|
|
}
|
|
|
|
|
}
|