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
Comments
Yes a custom query like |
@Flithor I'll answer you with some sample code on Stack Overflow, once you have posted your question over there. Please use the |
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;
}
}
} |
I want to check some table is existed, so how to get all table names?
use CreateCommand() to do a custom query?
The text was updated successfully, but these errors were encountered: