Home > Net >  How do I retrieve the creation date of a SQL Server database with EF Core?
How do I retrieve the creation date of a SQL Server database with EF Core?

Time:08-12

Is there a way for me to get the create_date property of sys.databases view in SQL Server with Entity Framework Core?

CodePudding user response:

It seems like there is no direct way from EF Core. I ended up using a DB Command for this purpose:

DateTime res = new DateTime();
using (var command = context.Database.GetDbConnection().CreateCommand())
{
    command.CommandText = "SELECT create_date FROM sys.databases WHERE [name] = db_name()";
    command.CommandType = CommandType.Text;

    context.Database.OpenConnection();

    using (var result = command.ExecuteReader())
    {
        while (result.Read())
        {
            res = (DateTime)result.GetValue("create_date");
        }
    }
}
return res;
  • Related