Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ中的动态继承映射_C#_Linq To Entities - Fatal编程技术网

C# LINQ中的动态继承映射

C# LINQ中的动态继承映射,c#,linq-to-entities,C#,Linq To Entities,这是一个继承映射,当我知道类型:Customer和Employee时,它就可以工作 modelBuilder.Entity() .Map(c=>c.Requires(“类型”).HasValue(“客户”)) .Map(e=>e.Requires(“Type”).HasValue(“Employee”)) .ToTable(“人”、“dbo”); 在我的例子中,Customer和Employee类将驻留在插件程序集中。因此,我尝试使用反射创建映射 注意:为了保持示例的简单性,我对类型进行了硬编

这是一个继承映射,当我知道类型:Customer和Employee时,它就可以工作

modelBuilder.Entity()
.Map(c=>c.Requires(“类型”).HasValue(“客户”))
.Map(e=>e.Requires(“Type”).HasValue(“Employee”))
.ToTable(“人”、“dbo”);
在我的例子中,Customer和Employee类将驻留在插件程序集中。因此,我尝试使用反射创建映射

注意:为了保持示例的简单性,我对类型进行了硬编码。另外,我只是尝试为客户类型创建映射

const BindingFlags binding=BindingFlags.Public | BindingFlags.Instance;
EntityTypeConfiguration entityConfig=modelBuilder.Entity();
var entityConfigType=entityConfig.GetType();
//映射方法
MethodInfo mapMethod=null;
var methods=entityConfigType.GetMethods(绑定);
foreach(方法中的var方法)
{
if(method.Name==“Map”&&method.IsGenericMethod)
{
mapMethod=method.MakeGenericMethod(typeof(Customer));
打破
}
}
//需要方法
类型[]requiresArgType=新类型[]
{
类型(字符串)
};
var mappingConfigType=typeof(EntityMappingConfiguration);
var requiresMethod=mappingConfigType.GetMethod(“Requires”,binding,null,requiresArgType,null);
//有价值方法
var vccType=typeof(ValueConditionConfiguration);
类型[]hasValueArgType=新类型[]
{
类型(字符串)
};
var hasValueMethod=vccType.GetMethod(“HasValue”,binding,null,hasValueArgType,null);
var param1=Expression.Parameter(typeof(EntityMappingConfiguration),“c”);
var requiresCall1=Expression.Call(param1,requiresMethod,Expression.Constant(“Type”);
var hasValueCall1=Expression.Call(requiresCall1,hasValueMethod,Expression.Constant(“客户”);
var lambda1=表达式.Lambda(hasValueCall1,param1);
var@delegate1=lambda1.Compile();
调用(entityConfig,新对象[])
{
@授权人1
});
尝试调用Map方法时,会引发以下异常:-

Object of type 'System.Func`2[System.Data.Entity.ModelConfiguration.Configuration.EntityMappingConfiguration`1[ConsoleApplication1.Customer],System.Data.Entity.ModelConfiguration.Configuration.StringColumnConfiguration]' cannot be converted to type 'System.Action`1[System.Data.Entity.ModelConfiguration.Configuration.EntityMappingConfiguration`1[ConsoleApplication1.Customer]]'.
我如何转换

Func<EntityMappingConfiguration<Customer>> with return type of StringColumnConfiguration
返回类型为StringColumnConfiguration的Func 到

动作
我更换了

var lambda1 = Expression.Lambda(hasValueCall1, param1);
有以下几点

MethodInfo[] expressionMethods = typeof (Expression).GetMethods(BindingFlags.Public | BindingFlags.Static);

MethodInfo lambdaMethod = null;
foreach (MethodInfo method in expressionMethods)
{
    if (method.Name == "Lambda" && method.IsGenericMethod && method.GetParameters().Length == 2)
    {
        lambdaMethod = method;
        break;
    }
}

MethodInfo genericLambdaMethod = lambdaMethod.MakeGenericMethod(typeof (Action<>).MakeGenericType(typeof<EntityMappingConfiguration<Customer>>));

LambdaExpression lambda1 = (LambdaExpression) genericLambdaMethod.Invoke(null, new object[]
{
    hasValueCall1, new[]
    {
        param1
    }
});
MethodInfo[]expressionMethods=typeof(Expression).GetMethods(BindingFlags.Public | BindingFlags.Static);
MethodInfo lambdaMethod=null;
foreach(expressionMethods中的MethodInfo方法)
{
if(method.Name==“Lambda”&&method.IsGenericMethod&&method.GetParameters()。长度==2)
{
lambdaMethod=方法;
打破
}
}
MethodInfo genericLambdaMethod=lambdaMethod.MakeGenericMethod(typeof(Action).MakeGenericType(typeof));
LambdaExpression lambda1=(LambdaExpression)genericLambdaMethod.Invoke(null,新对象[])
{
hasValueCall1,新[]
{
参数1
}
});

Lambda方法的通用变体起到了作用。您好,您介意在回答您自己的问题时详细说明这一点,并在以后将其标记为已接受吗?(别担心,这是StackOverflow的常见做法)。
MethodInfo[] expressionMethods = typeof (Expression).GetMethods(BindingFlags.Public | BindingFlags.Static);

MethodInfo lambdaMethod = null;
foreach (MethodInfo method in expressionMethods)
{
    if (method.Name == "Lambda" && method.IsGenericMethod && method.GetParameters().Length == 2)
    {
        lambdaMethod = method;
        break;
    }
}

MethodInfo genericLambdaMethod = lambdaMethod.MakeGenericMethod(typeof (Action<>).MakeGenericType(typeof<EntityMappingConfiguration<Customer>>));

LambdaExpression lambda1 = (LambdaExpression) genericLambdaMethod.Invoke(null, new object[]
{
    hasValueCall1, new[]
    {
        param1
    }
});