Entity framework core and get table list from sqlite database

c# entity-framework-core sqlite

Question

I want a list of tables that exists on sqlite database, and well I've got a simple query that looks like:

SELECT name from sqlite_master WHERE type='table';

But now I need to execute this using entity framework core, so I am binding this to DataContext, and here is where I am stuck as data context contains all representation of tables as db set but not sqlite system tables, I tried using query on exposed database facade but it is not returning any pother values then number of rows it has affected, something like:

using (var ctx = new DataContext())
{
    var query = @"SELECT name from sqlite_master WHERE type='table';"
    string[] result = ctx.Database.ExecuteSqlCommand(query);
    return result;
}

Is there any other way i can that information.

1
0
3/28/2019 10:04:21 AM

Accepted Answer

You can directly use ADO.NET commands with EntityFrameworkCore

using (var ctx = new DataContext())
{
    using (var command = ctx.Database.GetDbConnection().CreateCommand())
    {
        command.CommandText = "SELECT name from sqlite_master WHERE type='table'";
        ctx.Database.OpenConnection();
        using (var result = command.ExecuteReader())
        {
            while (result.Read())
            {
                Console.WriteLine(result.GetString(0));
            }
        }
    }
}
3
3/28/2019 10:57:17 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow