简体   繁体   English

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

[英]Entity framework core and get table list from sqlite database

I want a list of tables that exists on sqlite database, and well I've got a simple query that looks like:我想要一个存在于 sqlite 数据库中的表列表,我有一个简单的查询,如下所示:

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:但是现在我需要使用实体框架核心来执行它,所以我将它绑定到 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;
}

Is there any other way i can that information.有没有其他方法可以获取这些信息。

You can directly use ADO.NET commands with EntityFrameworkCore您可以直接将 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));
            }
        }
    }
}

In case you need a schema with a list of tables inside the database you can always do:如果您需要一个包含数据库内表列表的架构,您可以随时执行以下操作:

using (var ctx = new DataContext())
{
    DataTable dbSchemaWithTables = ctx.Database.GetDbConnection().GetSchema("Tables");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Entity Framework Core 从所有消息的数据库中获取用户对话框列表 - How to get a list of user dialogs from the database of all messages using Entity Framework Core 如何使用 Entity Framework Core 从子表列表的每个订单中获取产品行? - How to get product row from each order of child table list using Entity Framework Core? 从 Entity Framework Core 中的实体获取 DbContext - Get DbContext from Entity in Entity Framework Core 使用实体框架从修改后的列表更新数据库表 - Updating database table from modified list with Entity Framework 带有实体框架 6 的 SQLite“没有这样的表” - SQLite with Entity Framework 6 “no such table” 如何忽略 Entity Framework Core SQLite 数据库中的外键约束? - How to ignore Foreign Key Constraints in Entity Framework Core SQLite database? 使用实体框架在 ASP.NET 核心中为 SQLite 数据库设置 DateTimeKind - Setting DateTimeKind for SQLite database in ASP.NET Core with Entity Framework 使用 Entity Framework Core 和 .NET 6 更新 SQLite 数据库以反映对模型的更改 - Updating a SQLite Database to reflect a change to the model with Entity Framework Core and .NET 6 Entity Framework Core:获取所有表的列表 - Entity Framework Core: Get a List of All the Tables 如何使用 Entity Framework Core 从数据库表中确定通用实体的下一个主键? - How to determine the next primary key from a database table for a generic entity using Entity Framework Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM