实体框架核心并从sqlite数据库获取表列表

Woj*_*icz 2 c# sqlite entity-framework-core

我想要一个存在于 sqlite 数据库中的表列表,我有一个简单的查询,如下所示:

SELECT name from sqlite_master WHERE type='table';
Run Code Online (Sandbox Code Playgroud)

但是现在我需要使用实体框架核心来执行它,所以我将它绑定到 DataContext,这里是我被卡住的地方,因为数据上下文包含表的所有表示为 db set 但不是 sqlite 系统表,我尝试在公开的情况下使用查询数据库外观,但它没有返回任何 pother 值,然后是它影响的行数,例如:

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

有没有其他方法可以获取这些信息。

jgo*_*day 6

您可以直接将 ADO.NET 命令与 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));
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)