实体类
public class MyEntity {
public List<MyProperty> Properties;
public Type EntityType;
}
物业类
public class MyProperty {
public string PropertyName;
public Type PropertyType;
}
我有一个实体构建器类,它有一个创建新属性构建器的方法,并将现有的 MyProperty 实例传递给它。 它的调用方式如下:.Property(Blog x => x.Post)
public class MyEntityBuilder<TEntity> {
private MyEntity entity;
public MyPropertyBuilder<TProperty> Property<TProperty>(Expression<Func<TEntity, TProperty>> propertyExpression) {
MyProperty property = **Do something with the propertyExpression**
return new MyPropertyBuilder(property);
}
}
我尝试研究 EFCore 内部结构,但了解不多。基本上,我想将我的表达式编译成类似的东西:
MyProperty property = entity.Properties
.Single(x => x.PropertyType == typeof(TProperty) && x.PropertyName == propertyExpression.GetPropertyName())
我正在努力获取实体属性的名称,该名称是在表达式中传递的
找到答案。使用 Microsoft.EntityFrameworkCore.Infrastruct 命名空间的 GetPropertyAccess() 方法