如何获取自定义实体的属性,例如其在 EFCore OnModelCreating 中的完成方式

问题描述 投票:0回答:1

实体类

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())

我正在努力获取实体属性的名称,该名称是在表达式中传递的

c# reflection
1个回答
0
投票

找到答案。使用 Microsoft.EntityFrameworkCore.Infrastruct 命名空间的 GetPropertyAccess() 方法

© www.soinside.com 2019 - 2024. All rights reserved.