c# - LINQ 中的动态继承映射

标签 c# linq-to-entities

这是一个继承映射,当我知道类型:Customer 和 Employee 时会起作用。

modelBuilder.Entity<Person>()
            .Map<Customer>(c => c.Requires("Type").HasValue("Customer"))
            .Map<Employee>(e => e.Requires("Type").HasValue("Employee"))
            .ToTable("People", "dbo");

在我的例子中,CustomerEmployee 类将驻留在插件程序集中。所以,我正在尝试使用反射创建映射。

注意:为了使示例简单,我对类型进行了硬编码。另外,我只是想为 Customer 类型创建映射。

const BindingFlags binding = BindingFlags.Public | BindingFlags.Instance;

EntityTypeConfiguration<Person> entityConfig = modelBuilder.Entity<Person>();
var entityConfigType = entityConfig.GetType();

// Map Method
MethodInfo mapMethod = null;
var methods = entityConfigType.GetMethods(binding);
foreach (var method in methods)
{
    if (method.Name == "Map" && method.IsGenericMethod)
    {
        mapMethod = method.MakeGenericMethod(typeof(Customer));
        break;
    }
}

// Requires Method
Type[] requiresArgType = new Type[]
    {
        typeof(string)
    };

var mappingConfigType = typeof(EntityMappingConfiguration<Customer>);
var requiresMethod = mappingConfigType.GetMethod("Requires", binding, null, requiresArgType, null);

// Has Value Method
var vccType = typeof (ValueConditionConfiguration);
Type[] hasValueArgType = new Type[]
    {
        typeof(string)
    };

var hasValueMethod = vccType.GetMethod("HasValue", binding, null, hasValueArgType, null);

var param1 = Expression.Parameter(typeof (EntityMappingConfiguration<Customer>), "c");
var requiresCall1 = Expression.Call(param1, requiresMethod, Expression.Constant("Type"));
var hasValueCall1 = Expression.Call(requiresCall1, hasValueMethod, Expression.Constant("Customer"));

var lambda1 = Expression.Lambda(hasValueCall1, param1);
var @delegate1 = lambda1.Compile();

mapMethod.Invoke(entityConfig, new object[]
    {
        @delegate1
    });

当尝试调用 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

Action<EntityMappingConfiguration<Customer>>

最佳答案

我换了

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
    }
});

关于c# - LINQ 中的动态继承映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14544683/

相关文章:

C# TransactionScope - L2E

linq - 连接三个表并使用左外连接

entity-framework - 如何在 Linq 的 Where 条件中使用 IN 运算符

c# - LINQ 不能使用 string.contains?

c# - 流式传输到包中,打包到 WordDocument 中,然后再返回

c# - 如何将用户定义类型的数组传递给存储过程

c# - 按日期对实体组进行 Linq

c# - 陈旧元素引用 : element is not attached to the page document coded UI - C#

c# - 使用参数查询 MySQL & C#

c# - 正则表达式 - 查找匹配后的所有行 :