【发布时间】:2018-07-18 17:07:11
【问题描述】:
我正在尝试通过 c# 反射填充一对多关系。 我想要实现的是以下 EF 查询
_db.Entry(item).Collection(p => p.Valori).Query().OrderBy(o=>o.Ordinamento).Load();
我的要求是使用我自己的注释自动创建查询
[EagerLoad("Ordinamento")]
[InverseProperty("Attributo")]
public virtual ICollection<AttributoValore> Valori { get; set; }
直到 order by 语句一切正常
_db.Entry(entity).Collection(propertyInfo.Name).Load();
其中 entity 是包含我要填充的集合和 propertyInfo.Name == “Valori”的对象。
我找不到正确的语法来创建这段代码的 lamda 表达式
ParameterExpression pe = Expression.Parameter(type, "x");
Expression<Func<object, K>> expr = Expression.Lambda<Func<object, K>>(Expression.Property(pe, el.ordinamento), pe);
_db.Entry(entity)
.Collection<Object>(propertyInfo.Name)
.Query()
.OrderBy(expr)
.Load();
产生异常: System.ArgumentException: 'Jerp.AttributoValore' 类型的 ParameterExpression 不能用于'System.Object' 类型的委托参数' 有人可以帮帮我吗?
【问题讨论】:
标签: entity-framework-6 system.reflection