【发布时间】:2012-03-06 14:09:20
【问题描述】:
在 EF CodeFirst Fluent API 中,我可以编写如下内容:
modelBuilder.Entity<MyEntity>()
.Ignore(e => e.Property1);
modelBuilder.Entity<MyEntity>()
.Ignore(e => e.Property2);
如何忽略除一小部分之外的所有属性,如下所示:
modelBuilder.Entity<MyEntity>()
.IgnoreAllBut(e => e.ID, e => e.Important);
是否可以编写像IgnoreAllBut这样的扩展方法?
【问题讨论】:
-
是的,可以使用反射和调用忽略。这不是 fluent API 开箱即用的支持。您是否需要有关如何使用 Reflection 进行此操作的更详细帮助?
-
我会很感激帮助,因为当我用
Expression.Property()构造属性表达式并尝试在Ignore()中使用它时,我得到:The type arguments for method cannot be inferred from the usage. Try specifying the type arguments explicitly. -
将此表达式问题添加为新问题:stackoverflow.com/questions/9621736/…
标签: c# .net linq entity-framework ef-code-first