Pomelo.entityframeworkcore.mysql: 如何使用 Pomelo.EntityFrameworkCore.MySql 获取所有表名?

创建于 2020-12-24  ·  4评论  ·  资料来源: PomeloFoundation/Pomelo.EntityFrameworkCore.MySql

我想检查一些表是否存在,那么如何获取所有表名?

使用 CreateCommand() 进行自定义查询?

closed-question type-question

最有用的评论

哦哦,也许我不能,我在 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_SCHEMATABLES
其中TABLE_SCHEMA = 数据库();";

        var tableNames = new List<string>();

        using var reader = command.ExecuteReader();
        while (reader.Read())
        {
            tableNames.Add((string) reader["TABLE_NAME"]);
        }

        return tableNames;
    }
}

}
``

所有4条评论

是的,像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_SCHEMATABLES
其中TABLE_SCHEMA = 数据库();";

        var tableNames = new List<string>();

        using var reader = command.ExecuteReader();
        while (reader.Read())
        {
            tableNames.Add((string) reader["TABLE_NAME"]);
        }

        return tableNames;
    }
}

}
``

此页面是否有帮助?
0 / 5 - 0 等级