Skip to content

Instantly share code, notes, and snippets.

@peteraritchie
Last active September 12, 2026 15:44
Show Gist options
  • Select an option

  • Save peteraritchie/f9a9442f11e2e64fea09bdabc029cd69 to your computer and use it in GitHub Desktop.

Select an option

Save peteraritchie/f9a9442f11e2e64fea09bdabc029cd69 to your computer and use it in GitHub Desktop.
Get Sqlite database tables via a `DbContext` object #dotnet #csharp
public static Dictionary<string, string> GetDatabaseTables(DbContext db)
{
Dictionary<string, string> tablesDictionary = [];
var connection = db.Database.GetDbConnection();
using var command = connection.CreateCommand();
command.CommandText = "SELECT name, sql from sqlite_schema where type='table';";
using var reader = command.ExecuteReader();
while (reader.Read())
{
var entityName = reader.GetString(0);
var createSql = reader.IsDBNull(0) ? string.Empty : reader.GetString(1);
tablesDictionary[entityName] = createSql;
Debug.WriteLine($"{entityName}: {createSql}");
}
return tablesDictionary;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment