【发布时间】:2017-01-19 18:16:04
【问题描述】:
刚开始我是 Expression 和 Func 的新手。
我试图避免在我的 EF 映射类中出现重复代码,但我遇到了错误的数据库。
以下面的地图类为例:
public class EntityMap : EntityTypeConfiguration<Entity>
{
public EntityMap()
{
Property(x => x.PropertyA.Property);
Property(x => x.PropertyB.Property);
}
}
其中PropertyA 和PropertyB 是同一类型,并且具有许多属性
是否可以用一个简单的方法来重构它,在参数中传递 x => x.PropertyA 或 PropertyB 并执行类似 Property(x => x. methodParemeter Property); 的操作?如何 ?
该方法可能是这样的:
private void SubMap(Expression<Func<Entity, SubEntity>> propertyExpression, string prefix)
{
Property(x => x.propertyExpression.Property)
.HasColumnName(string.Format("{0}{1}", prefix,"columnName"));
}
【问题讨论】:
-
在什么方面会有所改进?
-
我不明白你想自动将classA属性映射到class B的问题
-
所以
.Property您的意思是例如.MaxLength(50),并且您希望在多个属性上使用更密集的语法来调用它?所以你想要{ f => f.PropertyA, f => f.PropertyB}.ForEach(p => p.MaxLength(50))这样的东西? -
我的建议.... 抛弃 EF 并获得 Dapper github.com/StackExchange/dapper-dot-net
-
@nurdyguy,我不能,而且 EF 也不是我的首选。
标签: c# .net entity-framework reflection lambda