[英]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.