Last active
September 12, 2026 15:44
-
-
Save peteraritchie/f9a9442f11e2e64fea09bdabc029cd69 to your computer and use it in GitHub Desktop.
Get Sqlite database tables via a `DbContext` object #dotnet #csharp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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