Home > database >  Entity Framework get sysdate from DUAL of Oracle
Entity Framework get sysdate from DUAL of Oracle

Time:11-03

What is the best approach to get SYSDATE from 'DUAL' of Oracle?

I tried to use ExecuteSqlRawAsync but I need return value as well.

public async Task<DateTime> GetTime()
{
  // This does not return value and does not work
  await _dbContext.Database.ExecuteSqlRawAsync("Select sysdate from dual;");
}

Another way is to create a modelbuilder like.

public partial class Dual 
{
  public DateTime SysDate { get; set; }
}

public virtual DbSet<Dual> Dual { get; set; }

modelBuilder.Entity<Dual>(e => { ... });

And use the query.

await _dbContext.Dual.FromSqlRaw("Select sysdate from dual;").ToListAsync();

Is there any simpler or better approach to get 'SYSDATE' from Oracle? I am using EF Core version 5.

CodePudding user response:

You can try the solution mentioned in this thread:

Entity Framework 7: get database time

public async Task<DateTime> GetTime()
{
    DbConnection connection = _dbContext.Database.GetDbConnection();
    if (connection.State == ConnectionState.Closed)
    {
        await connection.OpenAsync();
    }

    var command = connection.CreateCommand();
    command.CommandText = "select sysdate from dual";
    return (DateTime) await command.ExecuteScalarAsync();
}
  • Related