I want to check some table is existed, so how to get all table names?
use CreateCommand() to do a custom query?
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.
@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).
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)
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:
```c#
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;
}
}
}
```
Most helpful comment
@Flithor In that case, here is the promised sample code:
```c#
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; }
}
from
INFORMATION_SCHEMA
.TABLES
where
TABLE_SCHEMA
= database();";}
```