C# 遍历EF模型并获取属性名称

C# 遍历EF模型并获取属性名称,c#,entity-framework-core,C#,Entity Framework Core,我试图实现一个函数,该函数遍历EF模型并获取所有相关属性(列)的名称。假设EF中有以下类别: public class A { public Guid Id {get; set;} public int TotalNumber {get; set;} public string Name {get; set;} public virtual B classB {get; set;} public virtual ICollection<D> c

我试图实现一个函数,该函数遍历EF模型并获取所有相关属性(列)的名称。假设EF中有以下类别:

public class A {
    public Guid Id {get; set;}
    public int TotalNumber {get; set;}
    public string Name {get; set;}
    public virtual B classB {get; set;}
    public virtual ICollection<D> classesD {get; set;} = new List<D>();
}

public class B {
    public Guid Id {get; set;}
    public datetime Timestamp {get; set;}
    public string Description {get; set;}
    public virtual C Sender {get; set;}
    public virtual C Receiver {get; set;}
}

public class C {
    public Guid Id {get; set;}
    public string Name {get; set;}
}

public class D {
    public Guid Id {get; set;}
    public string ErrorMsg {get; set;}
    public datetime Timestamp {get; set;}
}
上述功能正常工作。但是,我不知道如何扩展它,使其也适用于子对象(子类)。我试图通过外键进入,但没有成功:

foreach (var foreignKey in entityType.GetForeignKeys())
{
    var tableName = _transactionDatabaseContext.Model.FindEntityType(foreignKey.GetType()).GetTableName(); // incorrect as foreignKey.GetType() returns something else
    //columnNames.AddRange(GetSqlColumnNames(tableName, true));
} 
有谁能建议,我怎么可能解决这个问题

//作为foreignKey不正确。GetType()返回其他内容
对。而不是

Model.FindEntityType(foreignKey.GetType())
使用属性

foreach(entityType.GetForeignKeys()中的var foreignKey)
{
var tableName=foreignKey.PrincipalEntityType.GetTableName();
//AddRange(GetSqlColumnNames(tableName,true));
}
请注意,您必须检查自引用并专门处理它们(基本上只处理一次每个唯一的FK),否则您将进入无限循环,并在堆栈结束时出现溢出异常

//作为foreignKey不正确。GetType()返回其他内容
对。而不是

Model.FindEntityType(foreignKey.GetType())
使用属性

foreach(entityType.GetForeignKeys()中的var foreignKey)
{
var tableName=foreignKey.PrincipalEntityType.GetTableName();
//AddRange(GetSqlColumnNames(tableName,true));
}
请注意,您必须检查自引用并专门处理它们(基本上只处理一次每个唯一的FK),否则您将进入无限循环,并在堆栈结束时出现溢出异常