1

I am trying to implement a function that traverses an EF Models and gets the names of all relevant properties (columns). Assume that there are the following classes in the 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;}
}

For class A, I should be able to get the following tuples: (TotalNumber, int), (Name, string), (classB.Timestamp, datetime), (classB.Description, string), (classB.Sender.Name. string), (classB.Receiver.Name, string). The class D can be ignored as it is a collection and I don't want to do that for collections.

At the first step, I implemented a function, which does this for a parent (main) class:

public virtual List<Tuple<string, string>> GetSqlColumnNames(string sqlTableName, bool addPrefix)
{
    var columnNames = new List<Tuple<string, string>>();
    var ignoredTypes = new List<string> { "uniqueidentifier"};
    var ignoredNames = new List<string> { "CreatedBy", "CreatedOn", "ModifiedBy", "ModifiedOn", "Version", "ValidFrom", "ValidTo"};

    foreach (var entityType in DBContext.Model.GetEntityTypes())
    {
        if (entityType.GetTableName()==sqlTableName)
        {
            foreach (var entityProperty in entityType.GetProperties())
            {
                if (!ignoredTypes.Contains(entityProperty.GetColumnType()) && !ignoredNames.Contains(entityProperty.GetColumnName()))
                {
                    columnNames.Add(new Tuple<string, string>(
                        addPrefix
                            ? sqlTableName + "." + entityProperty.GetColumnName()
                            : entityProperty.GetColumnName(), entityProperty.GetColumnType()));
                }
            }
            return columnNames;
        }
    }
    return columnNames;
}

The function above works as expected. However, I am not sure how to extend this in a way that it works also with subojects (subclasses). I tried to go via foreign keys, but no luck:

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));
} 

Can anyone suggest, how could I possibly resolve this?

1 Answer 1

0
// incorrect as foreignKey.GetType() returns something else

Correct. Instead of

Model.FindEntityType(foreignKey.GetType())

use PrincipalEntityType property

foreach (var foreignKey in entityType.GetForeignKeys())
{
    var tableName = foreignKey.PrincipalEntityType.GetTableName();
    //columnNames.AddRange(GetSqlColumnNames(tableName, true));
}

Note that you have to check for self references and handle them specially (basically process each unique FK only once), otherwise you will enter infinite loop, and at the end - stack overflow exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.