我想检查一些表是否存在,那么如何获取所有表名?
使用 CreateCommand() 进行自定义查询?
是的,像SHOW TABLES;
这样的自定义查询会起作用。 请在Stack Overflow上询问这个问题以获得进一步的帮助,因为问题并非用于此目的。
@Flithor我会在 Stack Overflow 上用一些示例代码回答你,一旦你在那里发布了你的问题。 请使用pomelo-entityframeworkcore-mysql
标签,这样我会收到通知(这是一个 MySQL 和 EF Core 相关问题,足够接近了)。
哦哦,也许我不能,我在 Stack Overflow 上有问题限制。 - 太多私人问题...
(我会编辑标题以帮助他人)
哦哦,也许我不能,我在 Stack Overflow 上有问题限制。 - 太多私人问题...
@Flithor在这种情况下,这是承诺的示例代码:
```c#
使用系统;
使用 System.Collections.Generic;
使用 System.Diagnostics;
使用 Microsoft.EntityFrameworkCore;
使用 Microsoft.Extensions.Logging;
使用 Pomelo.EntityFrameworkCore.MySql.Infrastructure;
命名空间 IssueConsoleTemplate
{
公开课冰淇淋
{
公共 int IceCreamId { 获取; 放; }
公共字符串名称 { 获取; 放; }
}
public class Context : DbContext
{
public DbSet<IceCream> IceCreams { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseMySql(
"server=127.0.0.1;port=3306;user=root;password=;database=Issue1279",
b => b.ServerVersion("8.0.21-mysql")
.CharSetBehavior(CharSetBehavior.NeverAppend))
.UseLoggerFactory(
LoggerFactory.Create(
b => b.AddConsole()
.AddFilter(level => level >= LogLevel.Information)))
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
}
}
internal static class Program
{
private static void Main()
{
using var context = new Context();
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
var tableNames = GetTableNames(context);
Trace.Assert(tableNames.Contains("IceCreams"));
}
private static List<string> GetTableNames(DbContext context)
{
// EF Core will close the connection automatically for us, when we ensure, that it is EF Core itself that is
// responsible for opening the database connection. We can ensure that by calling `OpenConnection()` first.
context.Database.OpenConnection();
var connection = context.Database.GetDbConnection();
using var command = connection.CreateCommand();
command.CommandText = @"select `TABLE_NAME`
来自INFORMATION_SCHEMA
。 TABLES
其中TABLE_SCHEMA
= 数据库();";
var tableNames = new List<string>();
using var reader = command.ExecuteReader();
while (reader.Read())
{
tableNames.Add((string) reader["TABLE_NAME"]);
}
return tableNames;
}
}
}
``
最有用的评论
@Flithor在这种情况下,这是承诺的示例代码:
```c#
使用系统;
使用 System.Collections.Generic;
使用 System.Diagnostics;
使用 Microsoft.EntityFrameworkCore;
使用 Microsoft.Extensions.Logging;
使用 Pomelo.EntityFrameworkCore.MySql.Infrastructure;
命名空间 IssueConsoleTemplate
{
公开课冰淇淋
{
公共 int IceCreamId { 获取; 放; }
公共字符串名称 { 获取; 放; }
}
来自
INFORMATION_SCHEMA
。TABLES
其中
TABLE_SCHEMA
= 数据库();";}
``