How to get sys.databases or sys.tables from sql in ef core

Viewed 836

I have a question what I couldn't resolve it for 2 days. I'm working on .NET Core 2.2 by the way and thats why I'm using Entity Framework Core.

I need to get sys.databases, sys.tables and sys.columns from sql but I couldn't reach them whatever I tried.

Someone advised me; Create a Stored Procedure and get them this way.

var result = dbcontext.Databases.FromSql("sp_GetDatabases").ToList();
  • I've tried but didn't reach because I don't want specific data in context. This only applies to data in DbContext.

So I didn't find how to get them. Need some help!

1 Answers
var connectionDb = _context.Database.GetDbConnection();
if (connectionDb.State.Equals(ConnectionState.Closed))
    connectionDb.Open();
object data = null;
using (var command = connectionDb.CreateCommand())
{
command.CommandText = @"Your query here";

data = command.ExecuteScalar();
}

Bit too late. But might help someone else out.

Related