Check table configurations in EF Core 3.1

To check the table configurations for EF Core 3.1, I did the following and it helped identify issues.

It iterates all tables in the context and selects one row. If there is an exception, it will catch it and display some related information. It might be helpful to also capture the stack trace.

    public string LoopAllTables(bool stopAfterFirstFail)
    {
        var exceptions = new StringBuilder();
        var propertyInfos = _dbContext.GetType().GetProperties().OrderBy(o => o.Name).ToArray();
        foreach (var propertyInfo in propertyInfos)
        {
            var property = propertyInfo.GetValue(_dbContext);
            if (property is IQueryable<object> tableQ)
            {
                try
                {
                    tableQ.Take(1).Load();
                }
                catch (Exception ex)
                {
                    exceptions.AppendLine(ex.Message + Environment.NewLine + "  from " + 
                       propertyInfo.Name);
                    if (stopAfterFirstFail)
                    {
                        break;
                    }
                }
            }
        }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.