Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get all table names with Pomelo.EntityFrameworkCore.MySql? #1279

Closed
Flithor opened this issue Dec 24, 2020 · 4 comments
Closed

How to get all table names with Pomelo.EntityFrameworkCore.MySql? #1279

Flithor opened this issue Dec 24, 2020 · 4 comments

Comments

@Flithor
Copy link

Flithor commented Dec 24, 2020

I want to check some table is existed, so how to get all table names?

use CreateCommand() to do a custom query?

@mguinness
Copy link
Collaborator

Yes a custom query like SHOW TABLES; would work. Please ask this question on Stack Overflow for further help as issues aren't meant for this purpose.

@lauxjpn
Copy link
Collaborator

lauxjpn commented Dec 24, 2020

@Flithor I'll answer you with some sample code on Stack Overflow, once you have posted your question over there. Please use the pomelo-entityframeworkcore-mysql tag, so I will be notified (its an MySQL and EF Core related question, so close enough).

@Flithor
Copy link
Author

Flithor commented Dec 24, 2020

Uh oh, maybe I can't, I've got question limit on Stack Overflow. - Too much personal question...
(I'll edited title for help others)

@Flithor Flithor changed the title How to get all table names? How to get all table names with Pomelo.EntityFrameworkCore.MySql? Dec 24, 2020
@lauxjpn
Copy link
Collaborator

lauxjpn commented Dec 24, 2020

Uh oh, maybe I can't, I've got question limit on Stack Overflow. - Too much personal question...

@Flithor In that case, here is the promised sample code:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;

namespace IssueConsoleTemplate
{
    public class IceCream
    {
        public int IceCreamId { get; set; }
        public string Name { get; set; } 
    }
    
    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`
from `INFORMATION_SCHEMA`.`TABLES`
where `TABLE_SCHEMA` = database();";
            
            var tableNames = new List<string>();

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

            return tableNames;
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants