EF Code First - Call MapInheritedProperties() for all entities by reflection

709 Views Asked by At

For my needs, I tried to force TPC (Table Per Concrete class) for my entities by calling the MapInheritedProperties() by using Reflection.

So what i want is to make calls like this one for all my entities in the OnModelCreating method of DbContext

modelBuilder.Entity<MyEntity>().Map(o => o.MapInheritedProperties())

What I tried to do is this but I don't succeed to create a delegate of the method MapInheritedProperties() cause I get the following exception : Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.

foreach (var feEntityType in this.GetEntityTypes(onlyConcreteClasses: true))
{
    // modelBuilder.Entity<Person>()
    var entityMethodInvoked = modelBuilder.GetType().GetMethod("Entity").MakeGenericMethod(feEntityType).Invoke(modelBuilder, null);

    // o.MapInheritedProperties()
    ParameterExpression parameterExpression = ParameterExpression.Parameter(typeof(EntityMappingConfiguration<>).MakeGenericType(feEntityType), "o");
    MethodInfo methodInfo = typeof(EntityMappingConfiguration<>).MakeGenericType(feEntityType).GetMethod("MapInheritedProperties", new Type[] { });
    Expression mapInheritedPropertiesMethodExpression = Expression.Call(parameterExpression, methodInfo);

    // Method Map<T>(...)
    var mapMethod = typeof(EntityTypeConfiguration<>)
        .MakeGenericType(feEntityType)
        .GetMethods()
        .Single(o => o.Name == "Map" && o.IsGenericMethod);

    // Func<EntityMappingConfiguration<MonEntite>>
    var mapMethodParameterType = typeof(Func<>).MakeGenericType(typeof(EntityMappingConfiguration<>).MakeGenericType(feEntityType));
    var mapAction = methodInfo.CreateDelegate(mapMethodParameterType); // <== DOESN'T WORK ==> Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.

    var mapMethodInvoked = mapMethod.MakeGenericMethod(feEntityType).Invoke(entityMethodInvoked, new[] { /* PARAMETER */ });
}

Here is how I get my concrete entities

protected IEnumerable<Type> GetEntityTypes(bool onlyConcreteClasses = false)
{
    var entityTypes = typeof(EntitiesLocation).Assembly.GetTypes().Where(o => o.GetInterfaces().Contains(typeof(IEntity)));

    foreach (var item in entitiesTypes)
    {
        if (onlyConcreteClasses && item.IsAbstract)
            continue;

        yield return item;
    }
}

Any idea about how I could create the delegate ? Or another way to call the method MapInheritedProperties() for all my entities without having to make it manually ?

1

There are 1 best solutions below

0
On
protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{
    //do whatever to get your entity Types
    var entityTypes = typeof(EntitiesLocation).Assembly.GetTypes()
        .Where(x => x.IsClass && !x.IsAbstract && x.GetInterfaces()
        .Any(i => i == typeof(IEntity)));

    foreach (var entityType in entityTypes)
    {
        GetType().GetMethod(nameof(MapInheritedProperties))
            .MakeGenericMethod(entityType)
            .Invoke(this, new object[] { modelBuilder });
    }
}

public void MapInheritedProperties<TEntity>(DbModelBuilder modelBuilder) where TEntity : class
{
    modelBuilder.Entity<TEntity>().Map(m => m.MapInheritedProperties());
}