C# EF Code First-通过反射为所有实体调用MapInheritedProperties()

C# EF Code First-通过反射为所有实体调用MapInheritedProperties(),c#,entity-framework,reflection,ef-code-first,code-first,C#,Entity Framework,Reflection,Ef Code First,Code First,出于我的需要,我尝试通过使用反射调用MapInheritedProperties()来强制实体使用TPC(每个具体类的表) 因此,我想要的是在DbContext的OnModelCreating方法中对我的所有实体进行这样的调用 modelBuilder.Entity<MyEntity>().Map(o => o.MapInheritedProperties()) modelBuilder.Entity().Map(o=>o.MapInheritedProperties())

出于我的需要,我尝试通过使用反射调用MapInheritedProperties()来强制实体使用TPC(每个具体类的表)

因此,我想要的是在DbContext的OnModelCreating方法中对我的所有实体进行这样的调用

modelBuilder.Entity<MyEntity>().Map(o => o.MapInheritedProperties())
modelBuilder.Entity().Map(o=>o.MapInheritedProperties())
我尝试这样做,但未能成功创建方法MapInheritedProperties()的委托,因为我遇到以下异常:无法绑定到目标方法,因为其签名或安全透明性与委托类型的签名或安全透明性不兼容

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 */ });
}
foreach(this.GetEntityTypes中的var feEntityType(仅限Concreteclasses:true))
{
//modelBuilder.Entity()
var entityMethodInvoked=modelBuilder.GetType().GetMethod(“实体”).MakeGenericMethod(feEntityType).Invoke(modelBuilder,null);
//o.MapInheritedProperties()
ParameterExpression ParameterExpression=ParameterExpression.Parameter(typeof(EntityMappingConfiguration).MakeGenericType(feEntityType),“o”);
MethodInfo MethodInfo=typeof(EntityMappingConfiguration).MakeGenericType(feEntityType).GetMethod(“MapInheritedProperties”,新类型[]{});
表达式mapInheritedPropertiesMethodExpression=Expression.Call(parameterExpression,methodInfo);
//方法映射(…)
var mapMethod=typeof(EntityTypeConfiguration)
.MakeGenericType(feEntityType)
.GetMethods()
.Single(o=>o.Name==“Map”&&o.IsGenericMethod);
//Func
var mapMethodParameterType=typeof(Func).MakeGenericType(typeof(EntityMappingConfiguration).MakeGenericType(feEntityType));
var mapAction=methodInfo.CreateDelegate(mapMethodParameterType);//
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
//尽一切努力获取实体类型
var entitytytypes=typeof(EntitiesLocation).Assembly.GetTypes()
.Where(x=>x.IsClass&!x.isastract&&x.GetInterfaces()
。任何(i=>i==typeof(IEntity));
foreach(entityTypes中的变量entityType)
{
GetType().GetMethod(名称(MapInheritedProperties))
.MakeGenericMethod(entityType)
.Invoke(这个新对象[]{modelBuilder});
}
}
public void MapInheritedProperties(DbModelBuilder modelBuilder),其中tenty:class
{
modelBuilder.Entity().Map(m=>m.MapInheritedProperties());
}
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;
    }
}
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());
}