C# 忽略具有PropertyInfo的属性
我想忽略一个属性信息如下的属性C# 忽略具有PropertyInfo的属性,c#,entity-framework,C#,Entity Framework,我想忽略一个属性信息如下的属性 PropertyInfo propertyInfo = typeof(GLAccount).GetProperty("ExampleProp"); modelBuilder.Entity<GLAccount>().Ignore(g => propertyInfo); PropertyInfo PropertyInfo=typeof(GLAccount).GetProperty(“ExampleProp”); modelBuilder.Entit
PropertyInfo propertyInfo = typeof(GLAccount).GetProperty("ExampleProp");
modelBuilder.Entity<GLAccount>().Ignore(g => propertyInfo);
PropertyInfo PropertyInfo=typeof(GLAccount).GetProperty(“ExampleProp”);
modelBuilder.Entity().Ignore(g=>propertyInfo);
上面的代码块给出了以下错误
The expression 'g => value(Dashboard.DAL.Context+<>c__DisplayClass16_0).propertyInfo' is not a valid property expression. The expression should represent a property: C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'.'
表达式“g=>value(Dashboard.DAL.Context+c\uu DisplayClass16\u 0).propertyInfo”不是有效的属性表达式。表达式应该表示一个属性:C#::“t=>t.MyProperty”VB.Net:“Function(t)t.MyProperty”。”
我怎样才能解决这个问题?谢谢。如果您使用的API需要一个表达式树,则需要手动构建lambda:
var p=Expression.Parameter(typeof(GLAccount));
var body=Expression.Property(p,propertyInfo);
var lambda=表达式lambda(body,p);
modelBuilder.Entity().Ignore(lambda);
不过,这里的问题是要知道SomeType
。我假设Ignore(…)
实际上是Ignore(…)
或类似的,其中TResult
需要与上面的SomeType
相同,它需要是ExampleProp
的返回类型。您可能需要在此处使用MakeGenericMethod
另外请注意,如果您没有使用propertyInfo
执行任何其他操作,您也可以使用Expression.Property(p,“ExampleProp”)
作为捷径
虽然,您可以简单地使用:
modelBuilder.Entity().Ignore(“ExampleProp”);
是否有理由使用属性信息,而不只是执行modelBuilder.Entity().Ignore(glaccount=>glaccount.ExampleProp)
或modelBuilder.Entity().Ignore(“ExampleProp”)代码>?